Setting standalone = 'YES' using XMLEventWriter

血红的双手。 提交于 2019-12-12 21:38:18

问题


I'm using 'XMLEventWriter' to generate an XML file in Java. Code snippet is as below:

XMLEventWriter writer = outputFactory.createXMLEventWriter(new FileWriter(outFile)); 
XMLEvent startEvent = eventFactory.createStartDocument("UTF-8","1.0",true);
writer.add(startEvent);

In spite of setting the third argument to 'true', I'm seeing that the generated XML document's header does not have standalone="YES"

Could anyone suggest the changes (if any) to be made to make standalone="yes" appear in the generated XML file's document header ?


回答1:


I was looking at the same issue (cf. java StAX - standalone property of StartDocument) and I found that is is unimplemented in my Java version:

package: com.sun.xml.internal.stream.writers

class XMLEventWriterImpl

public void add(javax.xml.stream.events.XMLEvent xMLEvent) {
  //...
  case XMLEvent.START_DOCUMENT :{
    //...
    StreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());

(note that standalone property is not used here)

Then in next call (class XMLStreamWriterImpl)

public void writeStartDocument(String encoding, String version)
    throws XMLStreamException {
    //Revisit : What about standalone ?
    //...

This is the original comment in the code, so it is unsupported yet unless implemented in Java.

My current java version is:

java version "1.7.0_79" OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.04.2) OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

Maybe it has been fixed in a later version ?

EDIT

Just tested with

java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

and the issue still exists.



来源:https://stackoverflow.com/questions/27437603/setting-standalone-yes-using-xmleventwriter

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