android xml parsing of inner tags



Here the following is my xml content



<response>
<session>online</session>
<sitting>true</sitting>
<balance>85694.6</balance>
<num>13</num>
<lastactivity>06:48:19 21/08/2014</lastactivity>
<seatlist>
<seat event="sit" num="13" brief="qc" userId="8970ca285d9c4e4d" showbalance="false" usertypecode="ROBT" ></seat>
<seat event="sit" num="12" brief="xz" userId="34c80c3c1dcf47f2" showbalance="false" usertypecode="ROBT" ></seat>
</seatlist>
<userid>8970ca285d9c4e4d</userid>
</response>


Here i am able to parse the terms such as session, sitting balance, user id


when i try to parse the seatlist i am getting empty values.


Following is my parse code



String res = "my xml content from server"
XmlParser parser = new XmlParser();
Document doc = parser.getDomElement(res); // getting DOM element
NodeList nl = doc.getElementsByTagName("response");

for (int i = 0; i < nl.getLength(); i++)
{

Element e = (Element) nl.item(i);
userId = String.valueOf(parser.getValue(e, "userid"));
out.setBalance(Double.valueOf(parser.getValue(e, "balance")));
out.setUserid(parser.getValue(e, "userid"));
out.setTableName(parser.getValue(e, "table_name"));
out.setSitting(Boolean.valueOf(parser.getValue(e, "sitting")));
seatList.add(parser.getValue(e, "seatlist"));
Log.e(""," ===== "+seatList);
}


Here is my xmlparser.class



public class XmlParser {

public XmlParser() {
// TODO Auto-generated constructor stub
}

public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {

DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);

} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}

return doc;
}

public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}

public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
}


How to parse the content within the seatlist tag


No comments:

Post a Comment