XML parsing Issue in android(Similar tags within a tag)




<A>
<B></B>
<B></B>
<C></C>
<D></D>
</A>


Issue when i am parsing MY xml parsing java code



doc = XMLfunctions.XMLfromString(data);
NodeList nodes = doc.getElementsByTagName("PROPERTY");
for (int i = 0; i<1; i++) {
Element e = (Element) nodes.item(i);

// Street Name

if (!("null").equals(XMLfunctions.getValue(e, "street"))) {
mainList.add(XMLfunctions.getValue(e, "street"));
arliststitname.add(XMLfunctions.getValue(e, "street"));


}

// Images
String strImag = "";
NodeList picnode = e.getElementsByTagName("picture");
Log.d("nooo", picnode.getLength()+"");
for(int j = 0; j < picnode.getLength(); j++ ){
Element picele= (Element) picnode.item(j);
if (!("null").equals(XMLfunctions.getValue(picele, "picture"))) {
strImag=strImag+";"+XMLfunctions.getValue(picele, "picture");

Log.d("imageUrlnew", strImag);

}


}


Problem occur when i am fetching value from picture Tags(Return NULL value)


Xml parse java code



public class XMLfunctions {

public final static Document XMLfromString(String xml) {

Document doc = null;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {

DocumentBuilder db = dbf.newDocumentBuilder();

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

} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}

return doc;

}

/**
* Returns element value
*
* @param elem
* element (it is XML tag)
* @return Element value otherwise empty String
*/
public final static String getElementValue(Node elem) {
Node kid;
String str = "";
if (elem != null) {
if (elem.hasChildNodes()) {
for (kid = elem.getFirstChild(); kid != null; kid = kid
.getNextSibling()) {
if (kid.getNodeType() == Node.TEXT_NODE) {
// Log.d("XMLFUNCTIONS", kid.getNodeValue());
str = str + kid.getNodeValue();
}
}
}
}
return str;
}



public static int numResults(Document doc) {
Node results = doc.getDocumentElement();
int res = -1;

try {
res = Integer.valueOf(results.getAttributes().getNamedItem("count")
.getNodeValue());
} catch (Exception e) {
res = -1;
}

return res;
}

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

}


Main issue is that I am not able to parse similar Tag within a single Tags.


Please Help Me


Thanks In Advance


No comments:

Post a Comment