Modifying XML Documents

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

However, you can “modify” the structure of a document when using a SAX parser simply by calling various callback methods yourself. For example, you can modify ch12_01.xml to create ch12_11.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 <FIST_NAME> element and then “create” a new element ...

Get Real World 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.