0

I have an XML file starting:

<?xml version="1.0"?>
<results>
    <result id="0001">
        <hometeam>
            <name>Dantooine Destroyers</name>
            <score>6</score>
        </hometeam>
        <awayteam>
            <name>Wayland Warriors</name>
            <score>0</score>
        </awayteam>
    </result>
    <result id="0002">
        <hometeam>
            <name>Dantooine Destroyers</name>
            <score>3</score>
        </hometeam>
        <awayteam>...

and in a java file:

if(event.isStartElement()){
    if(event.asStartElement().getName().getLocalPart().equals(HOME)){
        System.out.println("In hometeam"); // for testing purposes

        event = eventReader.nextEvent(); // I expect <name> element

        if(event.isStartElement()){ // <------------ FALSE
            if(event.asStartElement().getName().getLocalPart().equals(NAME)){....

I'd expect this if statement to be true for the <name> element but if I stick in a System.out.println(event.isStartElement()) I get FALSE....

Also event.getEventType() returns XMLEvent.CHARACTERS which I don't understand... Can anybody see why?

Feel free to make edits to tags/title and question if necessary.

3
  • 2
    Can't you use a framework instead of writing your own parser? I would use JAXB or XStream to let automatically generate Java objects. Commented Dec 19, 2011 at 13:26
  • my task to to write my own parser Commented Dec 19, 2011 at 13:29
  • I think he is using StAX (Streaming API for XML) based on SAX, hence the event-oriented code. Commented Dec 19, 2011 at 13:31

2 Answers 2

1

Characters means that next part of XML are well - characters (in your case newline and indentation) - low level parser is unqualified to discardthem for you. it delivers just raw events. It's your work to proces structure correctly.

Sign up to request clarification or add additional context in comments.

Comments

1

That .nextEvent() call is probably bringing in the whitespace between <hometeam> and <name>. Note that in XML, all character data between tags (even if it's whitespace or newlines) is also accessible from the API.

You can test this by printing the element.

You don't normally see that whitespace with DOM-based APIs (or you can easily ignore it) but with event-driven APIs (like SAX or StAX) you have to ignore it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.