7
我正在使用StAX,並且希望將模式位置添加到我的xml文件。達到此目的的最佳方法是什麼?如何使用StAX添加模式位置
我正在使用StAX,並且希望將模式位置添加到我的xml文件。達到此目的的最佳方法是什麼?如何使用StAX添加模式位置
如果您使用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()
。
問候, 最大
嗨馬克斯,我已經預料到這個解決方案。感謝您的澄清。 – Lars 2012-01-28 20:54:19
這對我有效,但XML我的XML沒有完全驗證。爲了解決我的驗證問題,我需要指定一個更新的模式實例:'http:// www.w3.org/2001/XMLSchema-instance'。 – Muel 2012-12-05 03:23:01