home
NEWS       BLOGS       FORUMS       NEWSLETTERS       RESEARCH       EVENTS       DIGITAL LIBRARY       CAREERS  
Network Computing Network Computing Powered by InformationWeek Business Technology Network

IMMERSE YOURSELF:

SOA

  |

Data Center

  |

802.11n

  |

Data Privacy

  |
APO  |

Virtualization

  |

NAC

  |

Security

  |

Network Mgmt

  |

Enterprise Apps

  |

Storage & Servers



Netdesign Manual

Part 2

Java XML Programmers Reference

Chapter 11: XML Tools for Information Appliances


September 24, 2001


Brought to you by:





Check It Out!

Attribute Methods

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:

<exec executable="runme.exe" dir="." failonerror="true"/>

The failonerror attribute is a Boolean attribute whose values are true and false. ColdFusion's <cfoutput> element also has a Boolean attribute:

<cfoutput query="MyQuery" group="id"
groupcasesensitive="no"/>

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:

<Request name="ItemDetail">
<Parameters>
<ItemId type="Integer">553</ItemId>
<ItemId type="Integer">554</ItemId>
</Parameters>
</Request>

First, we must read and parse the file:

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


PAGE: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | NEXT PAGE
 





Ready to take that job and shove it?

Function:

Keyword(s):

State:
SPONSOR
RECENT JOB POSTINGS
CAREER NEWS
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Ari Balogh was named to the post of chief technology officer as the companys for a "realignment" of employees.










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
 
ROLLING RIGHT ALONG
Follow key Network Computing Reviews from conception to completion. This Week: Holistic APM.



Network Computing Reports Emerging Enterprise Podcast Series: Secrets to Success








TechSearch


Microsite of the Week


Powerful Information at Your Fingertips



InformationWeek Business Technology Network
InformationWeekInformationWeek 500InformationWeek 500 ConferenceInformationWeek AnalyticsInformationWeek CIO
InformationWeek EventsInformationWeek ReportsInformationWeek MagazinebMightyByte and SwitchDark Reading
Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingNo Jitter
space
Techweb Events Network
InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0 ConferenceMobile Business ExpoSoftware ConferenceCSI - Computer Security Institute
Black HatGTECEnergy CampMashup CampStartup Camp
space
Light Reading Communications Network
Light ReadingLight Reading EuropeUnstrungLight Reading's Cable Digital NewsConstantinopleInternet Evolution
Heavy ReadingLight Reading Live!Light Reading InsiderEthernet ExpoOptical ExpoTeleco TVTower Technology Summit
space
Financial Technology Network
Advanced TradingBank Systems & TechnologyInsurance & TechnologyWall Street & TechnologyAccelerating Wall StreetBank Systems & Technology Executive SummitBuyside Trading SummitInsurance & Technology Executive Summit
space
Microsoft Technology Network
MSDN MagazineTechNetThe Architecture Journal
space
App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |  Advertising Contacts  |   Briefing Centers
Copyright © 2008  United Business Media LLC  |  Privacy Statement  |  Terms of Service  |  Your California Privacy Rights