O'Reilly logo

Real World XML by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required