writeStartElement has prvoide namespaceURI, why need another writeNamespace?
The cod开发者_如何学Ce:
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
writer.writeStartDocument("1.0");
writer.writeCharacters("\n");
writer.writeStartElement("ns1", "sample", "http://www.e.com/ns1");
writer.writeNamespace("ns1", "http://www.e.com/ns1");
writer.writeEndElement();
writer.writeEndDocument();
The result is:
<?xml version="1.0"?>
<ns1:sample xmlns:ns1="http://www.e.com/ns1"/>
If I delete writer.writeNamespace("ns1", "http://www.e.com/ns1");
, then the result is
<?xml version="1.0"?>
<ns1:sample/>
What's the effect of the namespaceURI
parameter of writeStartElement()
?
Following statement also does prefix binding for you:
writer.writeStartElement("ns1", "sample", "http://www.e.com/ns1");
If you then use statements without specifying prefix, it will know that prefix should be ns1
:
writer.writeStartElement("http://www.e.com/ns1", "sample2");
writer.writeEndElement();
As for writeNamespace
, you still need to write namespace declaration by yourself.
精彩评论