XML in programming - DOM

References for subjects 4-6

Recommendations:

XML in programs - introduction

Import project from XML05.zip to Eclipse. Look at examples in pl.mimuw.xml.rooms package. Run them on example file rooms1.xml.

DOM

Task 1.

Program DOMFormatter prints out a given document (element tags and text nodes only, we do not handle special characters, etc.).

  1. Remove all existing indentations and unnecessary whitespaces using e.g. String.trim
  2. Add an indentation before each element, proportionally to the depth of the element.
  3. Print also comments.

Example 1.

The following lines of code turn validation against XML Schema on.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new StreamSource("school.xsd"));
factory.setSchema(schema);
DocumentBuilder db = factory.newDocumentBuilder();

Task 2.

  1. Add validation to the program CountSeats_DOM_Specialized.
  2. Check the effect of specifying default attribute value. In the schema specify the default value of projector to false, to true and run the program each time.
  3. Check out what happens in case of a validation error.
  4. Make the program break (with a pretty message) in case of a validation error. Use your own ErrorHandler and setErrorHandler to register it before parsing.

Example 2.

The following line of code makes the parser namespace aware.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder db = factory.newDocumentBuilder();

Task 3.

Basing on CountSeats_DOM_Specialized create a program that...

  1. works well with documents rooms_ns1.xml and rooms_ns2.xml,
  2. does not count seats in document rooms1.xml (and generally does not count elements with unqualified or incorrect names),
  3. validates documents against school_ns.xsd,
  4. signals that document rooms1.xml is not valid.

Valid XHTML 1.1Valid CSS