I am parsing xml file from SDcard and display it in textview. whenever i tried to access, nothing will displayed
InputStream in = new FileInputStream("/sdcard/jokes.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(in);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName("jokes");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
System.out.println("" + fstNode);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = (NodeList) fstElmnt.getChild("item");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = ((Node) fstNmElmnt).getChildNodes();
System.out.println("Number of joke description : "+ ((Node) fstNm.item(0)).getNodeValue());
}
}
This is the code which i did to parse xml file from sdcard. can anyone tell where i made a mistake or if you have better option pls let me know.
Try this for your loop:
for (int s = 0; s < nodeList.getLength(); s++)
{
Node firstNode = nodeList.item(s);
NodeList nodes = firstNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);
//print node info
}
}
I think you just import wrong class because there is no method called "getChild" in org.w3c.dom.Element.
Please import "org.w3c.dom.Element;" in stead of "import android.sax.Element;"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.