XML in programming - DOM

References for subject 5

XML in programs - introduction

Import the project from XML05.zip to Eclipse. Look at the lecture examples in pl.mimuw.xml.rooms package. Run them on the 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 all 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.

Modification of documents

DOM allows us to modify the values of nodes and the structure of the tree.

setTextContent is the easiest way to set the new value of a simple (text-only) element, and setAttribute sets the value of an attribute.

Creating new nodes requires 1) to create the object using one of factory methods of the owner Document, and then 2) to add the object to the tree using appendChild or insertChild methods of the parent element.

Task 4.

(optional) Add the calculated information about the number of seats to the document in a new element result, as a child of rooms.

Write the document to a new file or to stdout using the methods that can be found e.g. in ...more_dom.DomLoadSave2.


Valid XHTML 1.1Valid CSS