Parse XML to a different format with SAX

304 Views Asked by At

I have a huge problem with parsing an XML file to a different format. I'm trying to get all the related data like stated in this link: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/ (I searched stackoverflow before and found this link)

I use the interface XMLReader to parse and the XML Serializer for the output.

I just need to convert my XML with a DTD to another XML with a different DTD. The difference is that, instead of elements from my source XML, most of the children are now attributes in the target XML. There are no new elements, only a different arrangement.

Has anyone an idea how to deal with the problem with a SAX parser?

1

There are 1 best solutions below

0
On

You can use XMLFilters for that. See Elliotte Rusty Harold's book for explanation and examples:

The basic idea of filters is that an XMLReader, instead of receiving XML text directly from a file, socket, or other source, receives already parsed events from another XMLReader. It can change these events before passing them along to the client application through the usual methods of ContentHandler and the other callback interfaces. For example, it can add a unique ID attribute to every element or delete all elements in the SVG namespace from the input stream.

BTW the mkyong tutorial glosses over how the characters method works, that tends to bite a lot of people when they find their element data getting truncated. There's a better tutorial on Oracle's site.