These methods allow you to get and set attributes on an XMLElement object. Remember that NanoXML stores attributes for each element as a java.util.Properties object.
public void addProperty(String key, Object value)
public void addProperty(String key, int value)
public void addProperty(String key, double value)
public Enumeration enumeratePropertyNames()
public String getProperty(String key)
public String getProperty(String key, String default)
public int getProperty(String key, int default)
public double getProperty(String key, double default)
public boolean getProperty(String key, String
trueVal, String falseVal, boolean default)
public Object getProperty(String key, java.util.Hashtable
valueSet, String defaultValue)
Once again, we see some of the quirks of this release of NanoXML: the setter for getProperty() is called addProperty() instead of setProperty(), this is fixed for NanoXML 2.0 Beta.
The first three methods allow you to add or set different types of properties to an element. enumeratePropertyNames() allows you to enumerate through the set of attributes for an element, while getProperty() returns specific values for known named attributes. The getProperty(String key) method returns null if the property doesn't exist for the element.
Arguments
Type
Effect
key
String
The name of the
attribute to lookup
value
int, double,
String
The value of the
attribute
default
int,
double,
String
The value that is
returned if the attribute doesn't exist
trueVal
String
The value of the
attribute which should be interpreted as representing true;
for example, yes,
true,
or 1.
This argument gives you the flexibility of specifying what value to use for
Boolean true
in the getProperty()
methods that returns a boolean. See Usage and Examples, below.
falseVal
String
The value of the
attribute that should be interpreted as representing false;
for instance, false or 0.
This argument gives you the flexibility of specifying what value to use for
Boolean false in the getProperty()
method that returns a boolean. See Usage and Examples, below.
valueSet
Java.util.
Hashtable
Stores the
attributes of the element as key value pairs.
Usage and Examples
Let's first take a look at how to use this somewhat non-intuitive method, which was removed from Nano XML 2.0:
public boolean getProperty(String key, String trueVal,
String falseVal, boolean default)
This helper method makes it easier to determine values for Boolean attributes. A Boolean attribute is an attribute with only a Boolean value. Take, for instance, the <exec> element in Jakarta's Ant project:
However, its values are yes and no. getProperty() allows us to test the value of Boolean attributes generically. For example, to retrieve the value of groupcasesensitive, whose default value is yes if not specified, from <cfoutput>, we would write:
boolean b = elem.getProperty("groupcasesensitive", "yes",
"no", false);
If groupcasesensitive isn't specified, the last parameter, false in this case, marks its default value. Another example would be if we were parsing Ant's <exec> element. We could write:
boolean b = elem.getProperty("failonerror", "true",
"false", false);
Note, however, that we could also write:
boolean b = (elem.getProperty("failonerror",
"false")).equalsIgnoreCase("false");
Now let's move to look at our previous XML document, request.xml. Here is an example that reads it and outputs the #PCDATA and type attribute value for each <ItemId> element. Recall the XML document looks like this:
BufferedReader br =
new BufferedReader(new FileReader("request.xml"));
XMLElement elem = new XMLElement();
elem.parseFromReader(br); //elem is the root node
Then, we enumerate through each child node of the root. If any child element is named , we output its type attribute and its #PCDATA content. If the type attribute doesn't exist for some reason, the default value unknown is used.
Enumeration e = elem.enumerateChildren();
while (e.hasMoreElements()) {
XMLElement child = (XMLElement)e.nextElement();
//is the child named ItemId?
if (child.getTagName().equals("ItemId")) {
System.out.print("Type = " + child.getProperty("type", "unknown"));
System.out.println(" and item id = " +
child.getContents());
}
}
Running our application against request.xml:
> java -classpath /usr/local/java/NanoXML/nanoxml.jar Request
Type = Integer and item id = 553
Type = Integer and item id = 554
REPORTS
Analyize In-Line NAC strategies and products.
ANALYTICS Plan and design your enterprise blade server deployments
InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Download Today