DOM and StAX will readily handle XML comments.
With SAX 2 you will need to register an extra handler, LexicalHandler, to be called for comments and other lexical events (CDATA, DTD, Entities). The JAXP adapter class DefaultHandler2 is an adapter for all SAX2 handlers, including the LexicalHandler.
Here's a little code snippet, that shows how to set up your SAX parser to print comments:
SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.setProperty( "http://xml.org/sax/properties/lexical-handler", new LexicalHandler() { public void comment(char[] ch, int start, int length) throws SAXException { System.out.println( "/* comment: " + String.valueOf(ch, start, length) + " */"); } public void startDTD(String name, String publicId, String systemId) throws SAXException { } public void endDTD() throws SAXException { } public void startEntity(String name) throws SAXException { } public void endEntity(String name) throws SAXException { } public void startCDATA() throws SAXException { } public void endCDATA() throws SAXException { } });
No comments:
Post a Comment