Reading XML, isEndElement is never true
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. :-??
Code :
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:
Code :
<?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>
Re: Reading XML, isEndElement is never true
I don't see any obvious problems. Are there any exceptions being thrown at runtime? I mention this because if I strip it down to bare essentials it works for me
Code java:
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = new FileInputStream(configFile);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
}
// 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")) {
System.out.println("Added to list");
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
Re: Reading XML, isEndElement is never true
The only exception that I get is after this function returns, when i try to use the list becuase the list it returns is empty.
The last System.out.println never prints, because the code never goes into that if block.
Thanks for looking at it :) i was starting to feel kind of crazy. i know how it *should* work, it's just not doing it!~X( When I get some time today I'm going to try to rewrite the xml file. Maybe there's an invisible ascii character in there throwing things off. That's about all i can come up with. Thanks again :)