Children Methods
These methods enable access to child elements. They are typically used after parsing a document. Remember that all elements in the document are represented as XMLElement objects.
There is no way of accessing "sibling" elements in NanoXML as there is in DOM; each element is a child of the element directly above it.
public int countChildren()
public java.util.Enumeration enumerateChildren()
These methods take no arguments. Interestingly enough, countChildren() only returns the number of children one level deep in the tree. The other method returns the children n-levels deep.
Child Methods
These methods perform operations on elements in the document. It is helpful to think of the structure in memory as a Document Object Model (DOM), even though it is not. Remember that all elements in the document are represented as XMLElement objects; this is similar to the Node interface in the package org.w3c.Dom. Again, there are no siblings in NanoXML - only children.
public void addChild(nanoxml.XMLElement child)
public void removeChild(nanoxml.XMLElement child)
public void removeChild(String key)
public void setTagName(String tagName)
public String getTagName()
public void setContent(String content)
public String getContents()
public String toString()
public void write(java.io.Writer writer)
public void write(java.io.Writer writer, int indent)
Some of the quirks of this version of NanoXML are apparent here: we have a method called setContent(), but its accessor is called getContents().This is resolved in NanoXML 2.0 beta. setTagName() and getTagName() set the element name. setContent() and getContents() set the #PCDATA of the node. The write() methods output the node and its subnodes to a java.io.Writer.
Usage and Examples
Here is an example of how an XML document can be generated and written to a java.io.Writer.
Example: Generating and Writing an XML Document
The XML we generate is a request from our auction site, for the detail information on item numbers 553 and 554.
Sample Request.xml
<?xml version="1.0" encoding="UTF-8"?>
<Request name="ItemDetail">
<Parameters/>
<ItemId type="Integer">553</ItemId>
<ItemId type="Integer">554</ItemId>
</Request>
NanoXML gives us no way to add processing instructions to a document, so we will output the prolog <?xml version="1.0" encoding="UTF-8"?> ourselves. The addProperty() methods haven't been introduced yet, but they will be in the next section.
Source Request.java
import nanoxml.XMLElement;
import java.io.*;
public class Request {
public static void main(String args[]) throws IOException {
//create the document and set the root
XMLElement root = new XMLElement();
root.setTagName("Request");
root.addProperty("name", "ItemDetail");
//create and set the first child
XMLElement child1 = new XMLElement();
child1.setTagName("Parameters");
root.addChild(child1);
//create and set next child
XMLElement child2 = new XMLElement();
child2.setTagName("ItemId");
child2.setContent("553");
child2.addProperty("type", "Integer");
root.addChild(child2);
//create and set the last child
XMLElement child3 = new XMLElement();
child3.setTagName("ItemId");
child3.setContent("554");
child3.addProperty("type", "Integer");
root.addChild(child3);
//create a writer and output the prolog
BufferedWriter bw = new BufferedWriter(new
FileWriter("Request.xml"));
bw.write(new String("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>"), 0, 38);
//output the document and close the writer
root.write(bw);
bw.close();
}
}
Compiling and Running
To compile Request.java, do the following from the directory in which Request.java is saved:
> javac -classpath \NanoXML\nanoxml.jar;. Request.java
To run Request.class, do the following:
> java -classpath \NanoXML\nano.xml.jar Request
The output is a file in the current directory called Request.xml. It should look like Request.xml in the beginning of this example.