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> element and then "create" a new element by calling the startElement, characters, and endElement callbacks myself:

public void endElement(String uri, String localName, String rawName)
{
    indent = indent.substring(0, indent.length() - 4);
    displayStrings[numberDisplayLines] = indent;
    displayStrings[numberDisplayLines] += "</";
    displayStrings[numberDisplayLines] += rawName;
    displayStrings[numberDisplayLines] += '>';
    numberDisplayLines++;

    if (rawName.equals("FIRST_NAME")) {
        startElement("", "MIDDLE_NAME", "MIDDLE_NAME", null);
        characters("XML".toCharArray(), 0, "XML".length());
        endElement("", "MIDDLE_NAME", "MIDDLE_NAME");
    }
}

In the main method, I'll write this new document out to customer2.xml:

public static void main(String args[])
{
    displayDocument(args[0]);

    try {
        FileWriter filewriter = new FileWriter("customer2.xml");

        for(int loopIndex = 0; loopIndex < numberDisplayLines; loopIndex++){
            filewriter.write(displayStrings[loopIndex].toCharArray());
            filewriter.write('
'),
        }

        filewriter.close();
    }
    catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

And that's it; here's what the resulting file, customer2.xml, looks like, with the new <MIDDLE_NAME> elements:

<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
    <CUSTOMER>
        <NAME>
            <LAST_NAME>
                Smith
            </LAST_NAME>
            <FIRST_NAME>
                Sam
            </FIRST_NAME>
            <MIDDLE_NAME>
                XML
            </MIDDLE_NAME>
        </NAME>
        <DATE>
            October 15, 2001
        </DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>
                    Tomatoes
                </PRODUCT>
                <NUMBER>
                    8
                </NUMBER>
                <PRICE>
                    $1.25
                </PRICE>
            </ITEM>
            <ITEM>
                <PRODUCT>
                    Oranges
                </PRODUCT>
                <NUMBER>
                    24
                </NUMBER>
                <PRICE>
                    $4.98
                </PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
    <CUSTOMER>
        <NAME>
            <LAST_NAME>
                Jones
            </LAST_NAME>
            <FIRST_NAME>
                Polly
            </FIRST_NAME>
            <MIDDLE_NAME>
                XML
            </MIDDLE_NAME>
        </NAME>
        <DATE>
            October 20, 2001
        </DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>
                    Bread
                </PRODUCT>
                <NUMBER>
                    12
                </NUMBER>
                <PRICE>
                    $14.95
                </PRICE>
            </ITEM>
            <ITEM>
                <PRODUCT>
                    Apples
                </PRODUCT>
                <NUMBER>
                    6
                </NUMBER>
                <PRICE>
                    $1.50
                </PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
    <CUSTOMER>
        <NAME>
            <LAST_NAME>
                Weber
            </LAST_NAME>
            <FIRST_NAME>
                Bill
            </FIRST_NAME>
            <MIDDLE_NAME>
                XML
            </MIDDLE_NAME>
        </NAME>
        <DATE>
            October 25, 2001
        </DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>
                    Asparagus
                </PRODUCT>
                <NUMBER>
                    12
                </NUMBER>
                <PRICE>
                    $2.95
                </PRICE>
            </ITEM>
            <ITEM>
                <PRODUCT>
                    Lettuce
                </PRODUCT>
                <NUMBER>
                    6
                </NUMBER>
                <PRICE>
                    $11.50
                </PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
</DOCUMENT>

That finishes our work with the XML for Java package and with Java for the moment. In the next chapter, I'm going to start taking a look at working with XSL transformations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.43.216