The XmlReader class will parse any legal XML file (containing a reference to its DTD) and produce a parsed tree of XmlElements which it inserts into the document. The application can then traverse the tree to extract data or to build its own internal data structure. Here is an example illustrating a typical use of the XmlReader to process an XML file:
XmlDocument document = new XmlDocument(new URL("file:/...")); XmlReader reader = new XmlReader(); reader.setVerbose(true); reader.parse(document); if (reader.errorCount > 0) { ...do something... return; } XmlElement root = document.getRoot(); for (Iterator i = root.elements(); i.hasNext();) { ... process each child element... }
To create an XML file, the application creates a tree of XmlElements and inserts it into an XmlDocument. It then calls the write() method with a Writer object. Here is an example illustrating a typical use:
XmlElement root = new XmlElement(); ... Add child elements to the root ... XmlDocument document = new XmlDocument("file:/..."); document.setRoot(root); XmlWriter writer = new XmlWriter(); writer.setVerbose(true); writer.write(document);