Your XMLFilter
should delegate to another ContentHandler
that serializes the document based on sax events.
SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance(); TransformerHandler serializer = factory.newTransformerHandler(); Result result = new StreamResult(...); serializer.setResult(result); XMLFilterImpl filter = new MyFilter(); filter.setContentHandler(serializer); XMLReader xmlreader = XMLReaderFactory.createXMLReader(); xmlreader.setFeature("http://xml.org/sax/features/namespaces", true); xmlreader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); xmlreader.setContentHandler(filter); xmlreader.parse(new InputSource(...));
Your callback should delegate a super
implementation that translates the events into serialization of the ContentHandler
.
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { super.startElement(namespaceURI, localName, qName, atts); ... }
In your endElement
you can check if you are in the last closing tag and add additional sax events.
public void endElement(String namespaceURI, String localName, String qName) throws SAXException { super.endElement(namespaceURI, localName, qName); if ("game".equals(localName)) { super.startElement("", "statistics", "statistics", new AttributesImpl()); char[] chars = String.valueOf(num).toCharArray(); super.characters(chars, 0, chars.length); super.endElement("", "statistics", "statistics"); } ... }
source share