Attribute Methods
These methods allow you to get, set, and remove attributes on an XMLElement object. Remember that NanoXML stores attributes as a hashtable in memory (actually it's a java.util.Properties object, but that's derived from Hashtable). This interface is much more intuitive than the version 1.6.7 interface.
public void getAttribute(String name)
public void getAttribute(String name, String default)
public void setAttribute(String name, String value)
public void removeAttribute(String name)
public Enumeration enumerateAttributeNames()
public boolean hasAttribute(String name)
public Properties getAttributes()
The removeAttribute() method is new for this release. If you're familiar with version 1.6.7, you'll notice that all of the extraneous getAttribute() methods that take different data types (int, double, String) are now gone. All attributes are now treated as Strings, a much simpler approach. All the method names also now use the xxxAttribute() convention instead of the xxxProperty() convention used in version 1.6.7. Again, this is more intuitive as the standard XML terminology for these items is attribute, not property.
The first two methods allow you to retrieve attributes of an element. The first method returns null if the attribute doesn't exist, while the second method returns default. enumeratePropertyNames() allows you to iterate through the set of attributes for an element, while getAttributes() returns the internal Properties structure to you directly.
| Arguments |
Type |
Effect |
|
name |
String |
The name of the attribute to lookup |
|
value |
String |
The value of the attribute |
|
default |
String |
The value that is returned if the attribute doesn't exist |
Class XMLParserFactory
net.n3.nanoxml
public class XMLParserFactory
extends java.lang.Object
This class provides convenient static methods for instantiating a parser, reader, builder, and validator all at once. These four objects interact with each other to parse a document. The parser object is the "glue" which contains the reader, builder, and validator. It is represented by the IXMLParser interface (see section Class StdXMLParser, page 612).
This class, XMLParserFactory, is not essential and could actually be removed from the library. It would save almost one kilobyte. The Usage and Examples section below shows how to do this. However, typical NanoXML 2.0 code that parses XML will start by calling one of these methods:
public static IXMLParser createDefaultXMLParser()
throws ClassNotFoundException, InstantiationException,
IllegalAccessException
public static IXMLParser createDefaultXMLParser(
IXMLBuilder builder, IXMLReader reader,
IXMLValidator validator)
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException
public static IXMLParser createXMLParser(
String className, IXMLBuilder builder,
IXMLReader reader, IXMLValidator validator)
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException
Note that all the creation methods are static, typical of a factory class. Usually you'll want to call the second method, createDefaultXMLParser(), to obtain an instance of StdXMLParser and call parse() to begin parsing. parse() returns the top-most XMLElement in the document, and you can now access the document data. You'll have to create an StdXMLBuilder with the Reader or String to be parsed before you call createDefaultXMLParser().
The first createDefaultXMLParser() method, although useful looking at first since it takes no parameters, actually isn't usable. The reason is because it creates a default StdXMLBuilder underneath the covers, and you have no way of telling that builder where your data source is! Hopefully, this will be resolved before the 2.0 Beta is finalized.
To summarize, here are the steps to parse a document using NanoXML 2.0 Beta:
- Create an StdXMLBuilder object, informing it of the Reader or String to be parsed.
- Call the static method createDefaultXMLParser(), passing it the builder created above, to obtain an IXMLParser instance.
- Call parse() on the IXMLParser instance.
- Iterate through the elements and attributes on the returned XMLElement to obtain your data (see Class XMLElement, page 605).
Arguments
| Arguments |
Type |
Effect |
| builder |
net.n3.nanoxml. IXMLBuilder |
The object that builds a tree of XMLElement nodes from a data source |
| reader |
net.n3.nanoxml. IXMLReader |
The objects that
reads the data to be parsed |
| validator |
net.n3.nanoxml.
IXMLValidator |
The object that
processes the DTD and resolves entity references. No document validation is
performed! |
| className |
String |
The name of the
class that implements IXMLParser |
Usage and Examples
Here is an example that creates a parser, reader, builder, and validator using the XMLParserFactory class.
StdXMLBuilder builder = new StdXMLBuilder();
IXMLParser parser = XMLParserFactory.createDefaultXMLParser(builder, new
StdXMLReader(new BufferedReader(new
FileReader("request.xml"))), new
NonValidator());
We could do the same thing without the XMLParserFactory class and remove it from our JAR, reducing its size from 19,692 bytes to 18,727 bytes (a savings of 965 bytes on our precious lightweight clients):
StdXMLBuilder builder = new StdXMLBuilder();
StdXMLParser parser = new StdXMLParser();
parser.setBuilder(builder);
parser.setValidator(new NonValidator());
parser.setReader(new StdXMLReader(new BufferedReader(new
FileReader("request.xml"))));
An important point if you choose to use the latter method and ignore XMLParserFactory is to make sure you call setBuilder(), setValidator(), and setReader() with some non-null object. Otherwise, a NullPointerException will be thrown.