next up previous
Next: Outputting XML Conventions Up: A command interpreter for Previous: Using the provided XML

Outputting XML using DOM

While you certainly could print out all the XML manually, it is much better in the long run to learn to use DOM to print out your XML. Again, the XmlUtility file we provide simplifies some of this process. You only need to know a few methods to easily do this project.

To create a Document object:

Document results = XmlUtility.getDocumentBuilder().newDocument();

To create an Element object:

Element elt = results.createElement(``elementName'');

To set an attribute for an Element:

elt.setAttribute(``attributeName'', ``attributeValue'');

To append an Element to another Node:

results.appendChild(elt);

To print out a Document to System.out:

XmlUtility.print(results);



MM Hugue 2019-05-28