Constructors
You no longer have to construct an XMLElement object unless you are building documents. When parsing documents, the object implementing IXMLBuilder (usually StdXMLBuilder) will provide the root element through its getResult() method (covered below). So you really only need to concern yourself with the following methods if you need to build documents with NanoXML.
public XMLElement()
public XMLElement(String name)
The default constructor is provided for #PCDATA text. To support mixed content in the XMLElement() class, #PCDATA is treated as an XMLElement object with no element name. We'll go into this in more detail in the Child Methods section (page 606), but this point is very important.
Use the default constructor XMLElement() for #PCDATA. Use the other constructors for element nodes.
The name argument represents the name of the new element.
Children Methods
These methods enable access to child elements. They are typically used after parsing a document. Note that all elements in a document, including #PCDATA text, are represented as XMLElement objects. There is no concept of siblings in NanoXML as there is in the Document Object Model (DOM). Each element is a child of the element directly above it.
public int getChildrenCount()
public boolean isLeaf()
public boolean hasChildren()
public Enumeration enumerateChildren()
public Vector getChildren()
public XMLElement getChildAtIndex(int index)
public XMLElement getFirstChildNamed(String name)
public Vector getChildrenNamed(String name)
public Vector getChildren()
Again we see the quirkiness of NanoXML: getChildrenCount() was called countChildren() in version 1.6.7. There is no apparent reason for the name change except perhaps to further the incompatibility between the two releases! Also, isLeaf()and hasChildren() are redundant methods, providing the same information.
The arguments name and index are the name or index of the desired child(ren). enumerateChildren() and getChildren() existed in version 1.6.7 and return an Enumeration or Vector of child XMLElements.
getChildAtIndex() will throw an ArrayIndexOutOfBoundsException if its index argument isn't valid. Likewise, getFirstChildNamed() will return null if no such child with element name name exists.
Usage and Examples
Here's an example that gets all elements named ItemId in an XML document fragment and outputs each element's #PCDATA content. We'll cover the getContent() method in the next section.
Enumeration enum =root.getChildrenNamed("ItemId").elements();
while (enum.hasMoreElements()) {
XMLElement elem = (XMLElement)enum.nextElement();
System.out.println("content is " + elem.getContent());
}
Now let's go over the methods for adding, removing, and accessing individual child elements.
Child Methods
public void addChild(XMLElement child)
public void removeChild(XMLElement child)
public void public void removeChildAtIndex(int index)
public void setContent(String content)
public String getContent()
addChild() adds an XMLElement to the document as a child of another element, while removeChild() and removeChildAtIndex() remove an element from a document. The latter provides a very simple XPath-style way of removing children.
setContent() and getContent() allow you to set the #PCDATA content between an element. It is important to know how these methods behave with regards to setName() and getName(), the functions used to get/set the name of an XMLElement. Using setContent() and getContent() incorrectly will break the XMLWriter class, which is used for outputting documents (see Class XMLWriter, page 613). If you create an XMLElement object with the constructor:
public XMLElement(String name)
this creates an element with name name. The correct way to add #PCDATA to this element is to create another XMLElement object using the default constructor:
public XMLElement()
then calling setContent() on the returned object, and adding that object to the first one using addChild(). If you instead call setContent() on the object returned by the named constructor, XMLWriter won't display subelements of that object.
To summarize, here is a code snippet that works just fine:
root = new XMLElement("Request");
XMLElement rootPCDATA = new XMLElement();
rootPCDATA.setContent("An Auction Request");
root.addChild(rootPCDATA);
XMLElement child1 = new XMLElement("Parameters");
root.addChild(child1);
XMLWriter writer = new XMLWriter(System.out); //output the
document
writer.write(root);
The output of this snippet is:
<Request>
An Auction Request
<Parameters/>
</Request>
and here is a code snippet that does not work fine (even though it looks like it should):
root = new XMLElement("Request");
root.setContent("An Auction Request");
child1 = new XMLElement("Parameters");
root.addChild(child1);
writer = new XMLWriter(System.out); //output the document
writer.write(root);
The output of this snippet is:
<Request>An Auction Request</Request>
You can see that the <Parameters> element is missing.