SAX Parser returns same element two times with 2nd time as empty



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 ?


No comments:

Post a Comment