2012-01-27 21 views

回答

10

如果您使用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()

問候, 最大

+0

嗨馬克斯,我已經預料到這個解決方案。感謝您的澄清。 – Lars 2012-01-28 20:54:19

+3

這對我有效,但XML我的XML沒有完全驗證。爲了解決我的驗證問題,我需要指定一個更新的模式實例:'http:// www.w3.org/2001/XMLSchema-instance'。 – Muel 2012-12-05 03:23:01