![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() | |
![]() |
|
||||||||||
About com.ms.xml.omThe Java based XML parser from Microsoft® loads XML documents and builds a tree structure of Element objects, starting with the root object of type Document. Each XML tag can either represent a node or a leaf of this tree. You can then browse and edit the tree using the methods of the Element class, and you can save the tree back out in XML format. The parser will also do DTD validation to ensure the XML is valid according to the specified XML DTD.
Loading a documentA document can be loaded by speciying a URL or an InputStream. For example:Document d = new Document(); try { URL url = new URL("http://www.w3.org/foo.xml"); d.load(url); } catch (ParseException e) { d.reportError(e, System.out); return; } ... You can then examine several properties of the document, like the name (getName) and id (getId) as specified in the <!DOCTYPE> tag.
Enumerating ChildrenYou can traverse the tree by first getting the root node (getRoot) and then enumerating through the children of the root using the Enumeration returned from the Element.getElements method, and then iterating through those children's children, etc.
In the following XML snippet:
The following Element hierarchy is constructed:
Note the new ignorable WHITESPACE nodes that capture the exact whitespace between tags. These nodes can be ignored and are only used by the Document.save() method to write the XML back out in the same format as it came in. You can override this formatting at save time by using the COMPACT or PRETTY save options.
If you are trying to write an application that finds the TITLE and
returns a string you could write the following:
Find Specific ChildrenYou can also construct an ElementEnumeration directly providing a tag name so that you can find all immediate children that have a matching tag name. For example, suppose you want to iterate through all CHANNEL elements that are immediate children of the document root:ElementEnumeration iter = new ElementEnumeration( document.getRoot(), Name.create("CHANNEL")); while (iter.hasMoreElements()) { Element e = (Element)iter.nextElement(); // Now you are guarenteed that 'e' // is a CHANNEL element. } Element Collections
An alternative way to enumerate children if you prefer array semantics
is to use the ElementCollection class. For example, to iterate
through all the CHANNEL tags using the element collection returned
from getChildren do the following:
You can also use this it iterate other things, like COMMENTS, by passing a null tag name and the flag Element.COMMENT. Enumerating AttributesYou can traverse the attributes of a given element also, using the getAttributes method. The following code finds the attribute whose value is a Name object:public Attribute findNameAttr(Element e) { AttributeEnumeration ae = e.getAttributes(); while (ae.hasMoreElements()) { Attribute a = ae.nextElement(); if (value instanceof Name) return a; } return null; }
Modifying the Document
You can modify the tree by using createElement and addChild. For
example to append a copyright notice at the end of a document, do
the following:
Modifying Attributes
Modifying attributes is easily done using the setAttribute and
removeAttribute methods. For example, if the COPYRIGHT was an
attribute of the root element instead of a child element you
could do the following:
Saving the document
You can now save your modified document back out to a given
OutputStream using the save method. For example, to
save the document to a file on disk do the following:
Namespaces
The Name objects returned from Element.getTagName and Attribute.getName
contain two fields, the name itself, and an optional namespace.
For example, if you have the following XML:
|
![]() |
© 1997 Microsoft Corporation. All rights reserved. Terms of use. |