XML and Java: A potent partnership, Part 1

Find out why XML and Java have captured the minds of enterprise application developers

1 2 Page 2
Page 2 of 2

<?xml version="1.0" ?>

<!DOCTYPE Order [ <!ELEMENT Order (OrderID, CustomerID, ProductID, Quantity, Options*) > <!ELEMENT OrderID (#PCDATA) > <!ELEMENT CustomerID (#PCDATA) > <!ELEMENT ProductID (#PCDATA) > <!ELEMENT Quantity (#PCDATA) > <!ELEMENT Options (Option1 | Option2 | Option3)+ > <!ELEMENT Option1 (Field1A) > <!ELEMENT Option2 (Field2A, Field2B) > <!ELEMENT Option3 EMPTY > <!ELEMENT Field1A (#PCDATA) > <!ELEMENT Field2A (#PCDATA) > <!ELEMENT Field2B (#PCDATA) > ]>

<Order> <OrderID>15</OrderID> <CustomerID>RULDS</CustomerID> <ProductID>DC123_44</ProductID> <Quantity>5</Quantity> <Options> <Option3/> <Option2> <Field2A>3</Field2A> <Field2B>black</Field2B> </Option2> <Option2> <Field2A>2</Field2A> <Field2B>white</Field2B> </Option2> </Options> </Order>

Figure 7. An XML-specified order

The XML in Figure 7 consists of two parts. First is the Document Type Definition (DTD). The DTD defines the structural relationship between the tags that comprise a document. This information allows a parser to rigorously and unambiguously validate a document.

The second part is an order that has been marked up with XML tags. You should immediately notice how easy it is to read and understand. Even in the absence of a DTD, the use of XML markup allows an XML parser to determine whether or not a document has the correct general form (or is "well-formed" in XML parlance).

Now consider the benefits:

  • Using XML results in less custom development. A validating XML parser can use a supplied DTD to automatically check the syntax of a document and enforce business rules. The application has only to validate the character data between the tags.

  • XML documents are self-documenting. The textual nature of XML tags and the inclusion of a well-defined DTD greatly reduce the amount of guesswork involved in developing a translator.

  • XML allows developers to create open, standardized interfaces for existing systems based on robust and widely available tools.

Wrapping it up

Before we go, lets look briefly at code that could be used to parse our XML order. We'll use SAX (Simple API for XML) and IBM's XML parser. (See Resources.)

import org.xml.sax.Parser; import org.xml.sax.Locator; import org.xml.sax.DocumentHandler; import org.xml.sax.ErrorHandler; import org.xml.sax.HandlerBase; import org.xml.sax.AttributeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

import org.xml.sax.helpers.ParserFactory;

public class TheParser { // This is the parser we will use to parse the XML. // It will be loaded dynamically.

private static final String _stringParserClass = "com.ibm.xml.parsers.ValidatingSAXParser";

public static void main(String [] rgstring) { try { // Create the parser.

Parser parser = ParserFactory.makeParser(_stringParserClass);

HandlerBase handlerbase = new HandlerBase() { public void startElement(String stringTagName, AttributeList attributelist) { if (stringTagName.equals("Order")) { // handle the <Order> tag } else if (stringTagName.equals("OrderID")) { // handle the <OrderID> tag } else if (stringTagName.equals("CustomerID")) { // handle the <CustomerID> tag } else if (stringTagName.equals("ProductID")) { // handle the <ProductID> tag } else if (stringTagName.equals("Quantity")) { // handle the <Quantity> tag } else if (stringTagName.equals("Option1")) { // handle the <Option1> tag } else if (stringTagName.equals("Option2")) { // handle the <Option2> tag } else if (stringTagName.equals("Option3")) { // handle the <Option3> tag } else if (stringTagName.equals("Field1A")) { // handle the <Field1A> tag } else if (stringTagName.equals("Field2A")) { // handle the <Field2A> tag } else if (stringTagName.equals("Field2B")) { // handle the <Field2B> tag } }

public void endElement (String stringTagName) { if (stringTagName.equals("Order")) { // handle the </Order> tag } else if (stringTagName.equals("OrderID")) { // handle the </OrderID> tag } else if (stringTagName.equals("CustomerID")) { // handle the </CustomerID> tag } else if (stringTagName.equals("ProductID")) { // handle the </ProductID> tag } else if (stringTagName.equals("Quantity")) { // handle the </Quantity> tag } else if (stringTagName.equals("Option1")) { // handle the </Option1> tag } else if (stringTagName.equals("Option2")) { // handle the </Option2> tag } else if (stringTagName.equals("Option3")) { // handle the </Option3> tag } else if (stringTagName.equals("Field1A")) { // handle the </Field1A> tag } else if (stringTagName.equals("Field2A")) { // handle the </Field2A> tag } else if (stringTagName.equals("Field2B")) { // handle the </Field2B> tag } }

public void characters(char [] rgc, int nStart, int nLength) { // handle character data }

public void error(SAXParseException saxparseexception) throws SAXException { throw saxparseexception; } };

parser.setDocumentHandler((DocumentHandler)handlerbase); parser.setErrorHandler((ErrorHandler)handlerbase);

for (int i = 0; i < rgstring.length; i++) { parser.parse(rgstring[i]); } } catch (Exception exception) { exception.printStackTrace(); } } }

The code in the example above illustrates how we would set up and use a SAX-compliant parser to parse and validate XML. Note that the code doesn't actually do anything with the parsed XML. You should notice, however, the lack of complicated validation logic. The XML parser, using the DTD, takes care of that for us.

Conclusion

I hope you've gained a better understanding of why XML is important in the enterprise -- it supports the enterprise application integration effort by providing a common, standardized platform upon which to build an integration infrastructure.

Next month, I'll continue my exploration of the space at the intersection of Java, XML, and the enterprise. It's fertile ground for the fruitful mind. Join me then!

Learn more about this topic

1 2 Page 2
Page 2 of 2