Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

xml - How to analyze a JAVA DOM Document using a SAX handler?

I am going to write a new functionality in my code that processes some XML data in a memory/CPU-efficient way. It will mainly analyze data from files, streams, byte arrays, etc., so the SAXParser seems to fits all of the above requirements.

Unfortunately, this new functionality will also need to analyze some XML data that is generated by older code that uses DOM solutions and returns Document classes.

Of course I could save that DOM Document to a file/stream/byte array etc. and then use SAXParser to process it, but such solution would require an additional memory space to save that data which is completely unnecessary from the data processing perspective.

Therefore I'm looking for some kind of DOM document crawler that reads already existing DOM data but uses SAX handlers to process it, which would allow me to both implement the basic processing logic only once in my custom SAX handler and also use any kind input data.

Have you encountered anything like this?

question from:https://stackoverflow.com/questions/66061876/how-to-analyze-a-java-dom-document-using-a-sax-handler

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You use an identity-transformation to feed XML from a DOM tree to a SAX Handler:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document),
                      new SAXResult(handler));

The DOMSource constructor takes a Node, so you can give it just part of a DOM document, i.e. a fragment.

The SAXResult constructor takes the ContentHandler that you would normally give to the XMLReader in the setContentHandler(...) call.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...