将冒号 (:) 放在 xml 属性中

putting colon (:) in xml attribute

我正在处理 android 项目,在任务的某些部分我需要创建 xml 文档文件。 我需要像这样创建包含冒号的属性

<APPLICAD_EXPORT xsi:noNamespaceSchemaLocation="file:///c:/temp/applicad-export.xsd">

我可以成功创建xml文件,但问题是我无法为属性APPLICAD_EXPORT添加冒号。

到目前为止,我确实喜欢这样获取属性前缀的冒号

XmlSerializer serializer = Xml.newSerializer();

            //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
            serializer.setOutput(fileos, "UTF-8");
            //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
            serializer.startDocument(null, Boolean.valueOf(true));
            //set indentation option
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            serializer.startTag(null, "APPLICAD_EXPORT");
            serializer.attribute(null, "xsi&#58;noNamespaceSchemaLocation", "file:///c:/temp/applicad-export.xsd");

但它显示错误 属性 xsi 的规范授权值

我想可能有一些方法可以做到这一点,但我想不出来。

冒号很特殊:您的属性名称由命名空间前缀(命名空间 URI 的 shorthand,在本例中为“http://www.w3.org/2001/XMLSchema-instance”)和本地名称组成,由一个冒号。

您对 XmlSerializer.attribute() 的调用应将命名空间 URI 指定为第一个参数,将本地名称 ("noNamespaceSchemaLocation") 指定为第二个参数。

您还需要使用 XmlSerializer.setPrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance") 将命名空间前缀绑定到 URI。