In this article, Martin Bond discusses XML and its associated APIs and standards, and how XML can be used to create flexible structured data that is inherently portable. This excerpt is from chapter (Day) 16 of Teach Yourself J2EE in 21 Days, second edition, by Martin Bond, et. al. (Sams, ISBN: 0672325586)
Today, you have had a very quick, and necessarily brief, introduction to XML and the APIs and technologies available in J2EE to parse and generate XML data. You have seen how XML can be used to create flexible structured data that is inherently portable. With DTDs and XML Schemas, you were shown how this data can also be validated. You have been introduced to several different ways of parsing an XML document with SAX, DOM, JAXP, or JAXB, and you should now recognize the advantages and disadvantages of each technique.
Tomorrow, you will extend your XML knowledge to include XML transformations.
Q&A
What are the major characteristics of XML?
XML is a human readable, structured data-encoding format that is generic, simple, flexible, extensible and free to use.
What is the difference between well-formed and valid XML.
Well-formed XML is syntactically and structurally correct. XML is only valid if it complies with the constraints of a DTD or XML schema.
What are the J2EE APIs and specifications that support the processing of XML?
The J2EE APIs and specifications that supports XML processing are JAXP (Java API for XML Processing), SAX (Simple API for XML Parsing), DOM (Document Object Model), and XLST for transforming XML documents
What are the main differences between SAX and DOM?
SAX provides a serial event-driven parser. DOM is more flexible in that it builds an in-memory representation of the document that can be manipulated randomly (that is, nodes can be addressed or processed in any order). SAX is generally more efficient (faster), while DOM can be a heavy user of memory.
Exercise
To practice working with XML, try the following two exercises; the first is relatively simple, but the second requires a little more effort.
Extend the WebDDBuilder application to optionally read in an existing web.xml file and add the new <servlet> and <servlet-mapping> elements from the servlet class and URL pattern information provided on the command line.
Hint: Most of the code is already in place. You will need to create another constructor to build the DOM tree from an existing DD whose filename is supplied as the last (optional) parameter on the command line. A simple web.xml file is provided in the Day16/solution/XML directory.
Tip - If your web.xml file has a non-local DTD specified in the DOCTYPE element (this will be the case if you are using a J2EE 1.3 or earlier web.xml file), you will require access to the Web for the parser to validate the XML.
Enhance your solution to check for duplicate servlet names. The servlet name in a Web Application DD must be unique. Ensure that the program will not add the same servlet class twice by checking for duplicate servlet names before adding the new entry.
A solution is provided in the Day 16 solution directory.
This chapter is from Teach Yourself J2EE in 21 Days, second edition, by Martin Bond et. al. (Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.