Generating a DOM Tree Scanner

And here we have the process for creating a basic Document Object Model (DOM) Tree Scanner. Like the SAX Document Handler above, it will list the attributes and text in our Inventory.xml document. But it will also make a small change to the attribute values to prove that the output is not exactly the same as the input. Our DOM Tree Scanner will change the input by prepending a coded abbreviation for each attribute's qualified name to the attribute's value. Finally, our DOM Tree Scanner will output XML data showing the modified attribute values.

We will start, just as with the SAX Document Handler, by generating the source, adding some code to display the input data, and running a test to verify the results. Then, we will code to output the attribute values and output the modified XML document. The output will be in the same format as the input but with slightly different data.

To generate the source, right-click the DTD file Inventory_Inventory and then select Generate DOM Tree Scanner from the context menu. Accept the default name Inventory_InventoryScanner. Add a main method. Type or paste Example 11-9 into Inventory_InventoryScanner.java. This code was adapted from the example in the comments near the top of the generated source, just after the package statement. Notice that similar comments are sprinkled throughout the source, code examples that could be uncommented to gain access to data parsed out of the XML input.

The generated source includes a method to process each element in the XML document, and each method includes logic to process the element's attributes. Each method is different, but they all have a structure similar to visitElement_Part shown in Table 11-1. We will augment the generated source as instructed in the following text and tables.

We need to add logic to print the name of each element. Search the source for occurrences of the following commented line. This line is one of the comments sprinkled throughout the source that shows how to access the data parsed from the XML input.

// element.getValue( );

Add the print statements shown in Table 11-1 right after the commented lines that were found. Or replace the commented lines with the new statements, if you prefer.

Table 11-1: Printing element names in Inventory_InventoryScanner

Method

Print statement

visitElement_Inventory

System.out.println("Element: Inventory");

visitElement_Kit

System.out.println("Element: Kit");

visitElement_Part

System.out.println("Element: Part");

visitElement_Supplier

System.out.println("Element: Supplier");

We need to add logic to print the attribute names and values. Search the source for occurrences of the following commented line. Again, this is one of the comments sprinkled throughout the source that shows how to access the data parsed from the XML input:

// attr.getValue( );

Add the print statements shown in Table 11-2 right after the commented lines that were found. Or replace the commented lines with the new statements, if you prefer.