How to read XML

Common methods of the XMLStreamReader object:

Method Description
hasNext() Returns true if there are more parsing events and false if there are no more events
next() Get next parsing event – a processor may return all contiguous character data in a single chunk, or it may split it into several chunks
getEventType() Returns an integer code that indicates the type of the event the cursor is pointing to
getLocalName() Returns the (local) name of the current event
getAttributeValue() Returns the normalized attribute value of the attribute with the namespace and localName If the namespaceURI is null the namespace is not checked for equality
getAttributeCount() Returns the count of attributes on this START_ELEMENT, this method is only valid on a START_ELEMENT or ATTRIBUTE
getAttributeLocalName(index) Returns the localName of the attribute at the provided index
getElementText() Reads the content of a text-only element, an exception is thrown if this is not a text-only element
getText() Returns the current value of the parse event as a string, this returns the string value of a CHARACTERS event, returns the value of a COMMENT, the replacement value for an ENTITY_REFERENCE, the string value of a CDATA section, the string value for a SPACE event, or the String value of the internal subset of the DTD

Common constants defined by the XMLStreamConstants interface:

Constant Description
START_ELEMENT The event is a start element.
END_ELEMENT The event is an end element
ATTRIBUTE The event is an attribute
CHARACTERS The event is character data
COMMENT The event is a comment
SPACE The event is whitespace
DTD The event is a DTD

Descriptions:

  • If the called method isn't valid for the current event, an XMLStreamException is thrown.
  • If the called method isn't valid for the event, an IllegalStateException is thrown. This will be an unchecked exception.

Code that reads the Product elements into a product array list.

ArrayList<Product> products = new ArrayList<Product>();
Product p = null;
while (reader.hasNext())
{
int eventType = reader.getEventType()
switch(eventType)
{
case XMLStreamConstants.START_ELEMENT:
String elementName = reader.getLocalName();
if (elementName.equals("Product"))
{
p = new product();
String code = reader.getAttributeValue(0);
p.setCode(code);
}
if (elementName.equals("Description"))
{
String description= reader.getElement()
p.setDescription=description;
}
if (elementName.equals("Price"))
{
String priceString = reader.getElement()
double price = Double.parseDouble(priceString);
p.setPrice=price;
}
break;
case XMLStreamConstants.END_ELEMENT:
elementName = reader.getLocalName()
if (elementName.equals("Product"))
{
products.add(p);
}
break;
default:
break;
}
reader.next()
}

A safer and more flexable way to read attributes.

int count = reader.getAttributeCount();
for (int i = 0; i < count; i++)
{
if (reader.getAttributeLocalName(i).equals("Code"))
{
String code = reader.getAttributeValue(i);
}
}

More info can be found here: http://www.j2ee.me/javase/6/docs/api/javax/xml/stream/XMLStreamReader.html