Applications loop over the entire document requesting for the Next Event. The Event Iterator API is implemented on top of Cursor API.
In this example we will read the following XML document and create objects from it.
Testing the program:
In this example we will read the following XML document and create objects from it.
<?xml version="1.0" encoding="UTF-8"?> <config> <item date="January 2009"> <mode>1</mode> <unit>900</unit> <current>1</current> <interactive>1</interactive> </item> <item date="February 2009"> <mode>2</mode> <unit>400</unit> <current>2</current> <interactive>5</interactive> </item> <item date="December 2009"> <mode>9</mode> <unit>5</unit> <current>100</current> <interactive>3</interactive> </item> </config>This the pojo class which holds the information stored by above xml file:
public class Item { private String date; private String mode; private String unit; private String current; private String interactive; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } public String getUnit() { return unit; } public void setUnit(String unit) { this.unit = unit; } public String getCurrent() { return current; } public void setCurrent(String current) { this.current = current; } public String getInteractive() { return interactive; } public void setInteractive(String interactive) { this.interactive = interactive; } @Override public String toString() { return "Item [current=" + current + ", date=" + date + ", interactive=" + interactive + ", mode=" + mode + ", unit=" + unit + "]"; } }The following reads the XML file and creates a List of object Items from the entries in the XML file.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; import de.vogella.xml.stax.model.Item; public class StaXParser { static final String DATE = "date"; static final String ITEM = "item"; static final String MODE = "mode"; static final String UNIT = "unit"; static final String CURRENT = "current"; static final String INTERACTIVE = "interactive"; @SuppressWarnings({ "unchecked", "null" }) public List<Item> readConfig(String configFile) { List<Item> items = new ArrayList<Item>(); try { // First create a new XMLInputFactory XMLInputFactory inputFactory = XMLInputFactory.newInstance(); // Setup a new eventReader InputStream in = new FileInputStream(configFile); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); // Read the XML document Item item = null; while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); // If we have a item element we create a new item if (startElement.getName().getLocalPart() == (ITEM)) { item = new Item(); // We read the attributes from this tag and add the date // attribute to our object Iterator<Attribute> attributes = startElement .getAttributes(); while (attributes.hasNext()) { Attribute attribute = attributes.next(); if (attribute.getName().toString().equals(DATE)) { item.setDate(attribute.getValue()); } } } if (event.isStartElement()) { if (event.asStartElement().getName().getLocalPart() .equals(MODE)) { event = eventReader.nextEvent(); item.setMode(event.asCharacters().getData()); continue; } } if (event.asStartElement().getName().getLocalPart() .equals(UNIT)) { event = eventReader.nextEvent(); item.setUnit(event.asCharacters().getData()); continue; } if (event.asStartElement().getName().getLocalPart() .equals(CURRENT)) { event = eventReader.nextEvent(); item.setCurrent(event.asCharacters().getData()); continue; } if (event.asStartElement().getName().getLocalPart() .equals(INTERACTIVE)) { event = eventReader.nextEvent(); item.setInteractive(event.asCharacters().getData()); continue; } } // If we reach the end of an item element we add it to the list if (event.isEndElement()) { EndElement endElement = event.asEndElement(); if (endElement.getName().getLocalPart() == (ITEM)) { items.add(item); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } return items; } }
Testing the program:
import java.util.List; import com.vani.xml.stax.model.Item; public class TestRead { public static void main(String args[]) { StaXParser read = new StaXParser(); List<Item> readConfig = read.readConfig("config.xml"); for (Item item : readConfig) { System.out.println(item); } } }
No comments:
Post a Comment