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