1

My Sax parser is doing weird thing when I am trying to parse simple xml file,

This is my xml file,

<?xml version="1.0"?>
<organization>
                <employee>
                                <title>Harry0</title>
                                <link>Smith0</link>
                                <date>hs0</date>
                                <salary>200000-0</salary>
                </employee>

                <employee>
                                <title>Harry1</title>
                                <link>Smith1</link>
                                <date>hs1</date>
                                <salary>300000-1</salary>
                </employee>

                 <employee>
                                <title>Harry2</title>
                                <link>Smith2</link>
                                <date>hs2</date>
                                <salary>300000-2</salary>
                </employee>
</organization>

Now If I want to read only the title element,

String elementValue = null;
String localName = null;
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
            this.localName = localName;
}


@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
        elementValue = new String(ch, start, length);
        if (localName.equals("title")){
            Log.e(localName,elementValue);
        }

}

and the output is,

10-19 14:41:02.261: E/title(7754): Harry0
10-19 14:41:02.261: E/title(7754):                                 
10-19 14:41:02.281: E/title(7754): Harry1
10-19 14:41:02.297: E/title(7754):                                 
10-19 14:41:02.297: E/title(7754): Harry2
10-19 14:41:02.297: E/title(7754):   

The tag name for the empty lines as well as the title line is same, i.e. title

Why Sax Parser is returning everything twice ?

1 Answer 1

1

You do not overwrite the endElement() method. characters() may be called multiple times und you should collect the data you are interested in, not just assign it during the first call of characters().

Then the parser hits the </title> tag but you don't see it and collect another chunk of chars until the next <link> tag. You should overwrite both startElement and endElement methods, collect your strings and report them when hitting the endElement()

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

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.