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, 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.


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 JitterPlug Into The Cloud
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 EvolutionPyramid Research
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