3

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>

i.e. 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

1 Answers1

0

EDIT: Just tried as described here i.e.

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 ...

tge
  • 91
  • 9
  • The "culprit" is StAXStream2SAX that is Xalan's default way of performing identity transform from Stax's XMLStreamReader sources. It converts comments as a no op (line 386 on my jdk8). There does not seem to be a way around that, except switching Source implementation or XSLT implementation. – GPI Oct 24 '16 at 12:41