the goal here is to read in an xml file and return a list of emailObjects.
my problem is that the last if statement never fires. this one: ( if (event.isEndElement()).
it's supposed to add the emailObject element to the list.
the emailObjects are being correctly instantiated, but they never get added to the list, because if(event.isEndElement()) is never true. So the list returns empty.
Here is the code and the xml sheet that its reading is at the bottom. i'm sure it's something simple i just can't seem to find it.
public List<emailObject> readConfig2(String configFile) { List<emailObject> items = new ArrayList(); try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); InputStream in = new FileInputStream(configFile); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); emailObject server = 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().equals("emailObject")) { server = new emailObject(); // 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(); System.out.println("attribute friendlyname is " + attribute.getName().toString()); if (attribute.getName().toString().equals(friendlyname)); server.setName(attribute.getValue()); System.out.println(server.getName() + " printed from server class"); } } if (event.isStartElement()) { if (event.asStartElement().getName().getLocalPart().equals(address)) { event = eventReader.nextEvent(); server.setAddress(event.asCharacters().getData()); System.out.println(server.getAddress()); continue; } } if (event.asStartElement().getName().getLocalPart().equals(active)) { event = eventReader.nextEvent(); server.setActive(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(); System.out.println("this end element is " + endElement.getName().getLocalPart().toString()); if (endElement.getName().getLocalPart().equals("emailObject")) { // items.addElement(server); items.add(server); System.out.println("Added to list"); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } return items; }
here is te xml sheet i'm reading:
<?xml version="1.0" encoding="UTF-8"?> <root> <config> <emailObject friendlyname="Angela Txt Msg"> <address>number@vtext.com</address> <active>true</active> </emailObject> <emailObject friendlyname="Angela Email"> <address>angelah@emaill.org</address> <active>true</active> </emailObject> </config> </root>