main in XMLToCSVBasic.java

Let's start with this bit of pseudocode:

Set up DOM XML environment (dependent on implementation)
Load input XML Document (dependent on implementation)

In JAXP, we build a DOM Document with the DocumentBuilder class. However, we can't just declare a DocumentBuilder; we must get one from a DocumentBuilderFactory. A real-world analogy would be a metal worker who wants to build a barbecue for his backyard. He needs tools to build it, and the ultimate source of those tools is a tool and die maker. So, we declare a tool and die maker (the DocumentBuilderFactory), who then makes us a tool (the DocumentBuilder) that we can use to build our barbecue (the Document). Here's how the Java code looks.

From XMLToCSVBasic.java
//  Set up DOM XML environment
DocumentBuilderFactory Factory =
    DocumentBuilderFactory.newInstance();

//  Create the new document builder
DocumentBuilder Builder = Factory.newDocumentBuilder();

//  Set the error handler
Builder.setErrorHandler(new SAXErrorHandler());

//  Load input XML Document (dependent on implementation)
//  Use our DocumentBuilder's parse method on the
//  disk file passed on the command line.

docInput = Builder.parse(new File(sInputXMLName));

The Builder.parse call is where we start making the barbecue. Also note the Builder.setErrorHandler call. We'll talk more about that soon.

The rest of the DOM-related code is pretty straightforward. Here's the Java code with the pseudocode embedded as comments.

//  NodeList of Rows <- Call Document's
//    getElementsByTagName for all elements named Row
RowList = docInput.getElementsByTagName("Row");

//  DO until Rows NodeList.item[index] is null
//    Call CSVRowWriter write method, passing
//        NodeList.item[index]
//    Increment index
//  ENDDO
while (RowList.item(iRows) != null)
{
  RowWriter.write(RowList.item(iRows));
  iRows++;
}

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

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