How to add a schema location with StAX

烂漫一生 提交于 2019-12-23 08:57:10

问题


I am using StAX and I want to add a schema location to my xml file. What is the best way to achieve this?


回答1:


If you use XMLStreamWriter, you can just use writeNamespace() and writeAttribute() (or just writeAttribute()).

XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeStartElement("YourRootElement");
xmlStreamWriter.writeNamespace("xsi", "http://www.w3.org/2000/10/XMLSchema-instance");
xmlStreamWriter.writeAttribute("http://www.w3.org/2000/10/XMLSchema-instance", "noNamespaceSchemaLocation",
        "path_to_your.xsd");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.flush();

Output:

<?xml version="1.0" ?>
<YourRootElement xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="path_to_your.xsd"></YourRootElement>

For XMLEventWriter, you should be able to do it by add()ing a createAttribute().

Regards, Max



来源:https://stackoverflow.com/questions/9031934/how-to-add-a-schema-location-with-stax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!