【发布时间】:2012-02-20 08:41:11
【问题描述】:
我正在使用 StAX,我想将架构位置添加到我的 xml 文件中。实现这一目标的最佳方法是什么?
【问题讨论】:
我正在使用 StAX,我想将架构位置添加到我的 xml 文件中。实现这一目标的最佳方法是什么?
【问题讨论】:
如果您使用XMLStreamWriter,您可以只使用writeNamespace() 和writeAttribute()(或只使用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();
输出:
<?xml version="1.0" ?>
<YourRootElement xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="path_to_your.xsd"></YourRootElement>
对于XMLEventWriter,您应该可以通过add()@createAttribute() 来完成。
问候, 最大
【讨论】:
http://www.w3.org/2001/XMLSchema-instance。