Now, configure execution parameters for the scanner, just as you did in the previous section for the parser. Open the Properties sheet for Inventory_InventoryScanner.java, click the Execution tab, and set the Arguments property to Inventory.xml. Invoke the Customizer dialog for the Executor property by clicking the value and then the ... button, click the Expert tab of the Customizer dialog to expose the Working Directory property, and set Working Directory to the directory that contains the Inventory.xml, the same as the source directory. It's time to test. Compile and execute Inventory_InventoryScanner. The results should match Example 11-8.

Next, add code to make a slight modification to the data values, just enough to prove that they're different when the new XML is output later. Locate the statements that you added to print the attribute values. Before each statement that you located, add a new statement to change the value of the data. We want to prove that the data was changed when it's output again later. Add statements that invoke attr.setValue as shown in Table 11-3. For example, to see what visitElement_Part looks like with all changes in place, see Table 11-4.

Table 11-4: Modifying attribute values in Inventory_InventoryScanner

Method

Attribute

Statement to modify data

visitElement_Inventory

Description

attr.setValue("ID:" + attr.getValue( ));

visitElement_Kit

Description

attr.setValue("KD:" + attr.getValue( ));

visitElement_Part

Description

attr.setValue("PD:" + attr.getValue( ));

visitElement_Part

Color

attr.setValue("PC:" + attr.getValue( ));

visitElement_Part

Size

attr.setValue("PS:" + attr.getValue( ));

visitElement_Supplier

Description

attr.setValue("SD:" + attr.getValue( ));

Near the end of the visitElement_Part method locate the statement that prints the value of data in a Part node. Before the print statement add a statement invoking node.setData to change the text data, as shown in Example 11-10. The entire method is given to show all the added code in context.

We're almost done. Let's take a moment to compile and test. The new output should show our modifications to the input data.

Finally, we'll add more code to output the modified XML data. We will use a Transformer object to copy the document with its modified data to an output stream. Add code to the main method so that it matches Example 11-11.

Compile and execute. Your output window should match Example 11-13 (except for a couple of line breaks added for readability). At the top is the data extracted and then modified from the input XML document. Next is the new XML document showing the modified data.

These examples give the basics of reading, modifying, and writing XML documents with Java classes. NetBeans makes it easy to access XML documents wherever you find them or to generate your own whenever you need to share data with other applications. Just remember to use a SAX document handler if serial read access is adequate, especially when speed and memory are critical. Use a DOM tree scanner if you need random access to the data elements in the XML document, if you need to update the data, or if you need to generate a new document. SAX document handlers are widely used in web applications, where resources are tight. Keep that in mind while you're reading the next chapter.