Modifying XML Documents

In the previous chapter, we saw that the XML for Java DOM parser has several methods that let you modify a document in memory, such as insertBefore and addChild, and so on. SAX parsers don't give you access to the whole document tree at once, so no similar methods exist here.

However, if you want, you can "modify" the structure of a document when using a SAX parser simply by calling various callback methods yourself. In the previous chapter, I modified customer.xml to create customer2.xml, adding a <MIDDLE_NAME> element with the text XML to each <PERSON> element in addition to the <FIRST_NAME> and <LAST_NAME> elements. It's easy enough to do the same here using SAX methods. All I have to do is to wait for a <FIRST_NAME> ...

Get Inside XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.