I\'ve got this xml:
try {
File SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsoluteFile();
File myDir = new File(SDCardRoot.getAbsolutePath() + "/.ABC"
+ "/.Config");
File file = new File(myDir, "config.xml");
InputStream inputSource = new FileInputStream(file.getPath());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputSource);
Element element = doc.getDocumentElement();
element.normalize();
NodeList nList = doc.getElementsByTagName("item");
for (int i = 0; i < nList.getLength(); i++) {
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
String id = eElement.getElementsByTagName("id").item(0)
.getTextContent();
eElement.getElementsByTagName("name").item(0)
.getTextContent();
eElement.getElementsByTagName("image_updated").item(0)
.getTextContent();
eElement.getElementsByTagName("image").item(0)
.getTextContent();
eElement.getElementsByTagName("colorcode").item(0)
.getTextContent();
}
}
} catch (Exception e) {
e.printStackTrace();
}
XML parsing. How can I get child's child?
You've a working example. Its java based, and working flawlessly in an Android device.
Finally i got Solution like this I have used Dom parsing
public class MainActivity extends Activity {
ArrayList<String> mImageLink;
ArrayList<String> phone_no;
int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageLink = new ArrayList<String>();
phone_no = new ArrayList<String>();
try {
File file = new File("mnt/sdcard/Backup_Apps/call_logs/calllog_35777569.xml");
InputStream is = new FileInputStream(file.getPath());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(is));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("alllogs");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
mImageLink.add(fstElmnt.getAttribute("count"));
count = Integer.parseInt(mImageLink.get(i));
}
NodeList n = doc.getElementsByTagName("log");
for (int j = 0; j < count; j++) {
Node node = n.item(j);
Element fstElmnt = (Element) node;
phone_no.add(fstElmnt.getAttribute("number"));
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
}