简体   繁体   中英

Why does XMLStreamReader/StAXSource strip comments from XML?

The code below (based on sample code from http://jax-ws.java.net/nonav/jax-ws-20-fcs/arch/com/sun/xml/ws/util/xml/StAXSource.html )

String xml = "<a><b>a text</b><!--a comment--><b/></a>";
StringReader sr = new StringReader(xml);
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(sr);
Source source = new StAXSource(reader);     
//Source source = new StreamSource(sr);
Result result = new StreamResult(System.out);      
TransformerFactory.newInstance().newTransformer().transform(source, result);

yields the following result:

<?xml version="1.0" encoding="UTF-8"?><a><b>a text</b><b/></a>

ie it strips out the xml comment. If I replace the StAXSource/XMLStreamReader with the StreamSource the comment is preserved.

Does anyone know why the XMLStreamReader/StAXSource combination strips them out and if there is any way to prevent it? The testing was done in 1.6 and 1.7 environments with no third party jars, so the XMLStreamReader becomes a

com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl 

Thanks

EDIT: Just tried as described here ie

case XMLStreamConstants.COMMENT:
  System.out.print("<!--");
  if (xmlr.hasText())
     System.out.print(xmlr.getText());
...

and it DOES read the comments. This does not answer the original question yet though ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM