I'm parsing XML which is on the server I read it and parse it There is no any error But I'm unable to see the data.
Here is my XML:
<BookData><book><Title><![CDATA[ABC]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book><book><Title><![CDATA[XYZ]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book>
I'm using DocumentBuilderFactory
see the code even I set
dbf.setCoalescing(true);
But still not working please see the code for DocumentBuilderFactory
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.d("XML parse Error:", e.getMessage());
return null;
} catch (SAXException e) {
Log.d("Wrong XML File Structure", e.getMessage());
return null;
} catch (IOException e) {
Log.d("IOException", e.getMessage());
return null;
}
Try this, you just have to pass InputSource instance to this method and it works.
private void DOMParser(InputSource inputSource) {
DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = dBuilderFactory.newDocumentBuilder();
Document dom = documentBuilder.parse(inputSource);
// get the root element.....
Element docElement = dom.getDocumentElement();
Log.i("Root Element", docElement.getTagName());
// now get the NodeList of root elements
NodeList nodeList = docElement.getElementsByTagName("book");
Log.i("NodeList Length", nodeList.getLength()+"");
for (int i = 0; i < nodeList.getLength(); i++) {
Element eleBook = (Element) nodeList.item(i);
Log.i("Book Node", eleBook.getTagName());
NodeList titleNode = eleBook.getElementsByTagName("Title");
Element TitleEle = (Element) titleNode.item(0);
Log.i("Title", "Title - "+TitleEle.getFirstChild().getNodeValue());
NodeList AuthorFName1Node = eleBook.getElementsByTagName("AuthorFName1");
Element AuthorFName1Ele = (Element) AuthorFName1Node.item(0);
Log.i("AuthorFName1","AuthorFName1 - "+AuthorFName1Ele.getFirstChild().getNodeValue());
NodeList AuthorFName11Node = eleBook.getElementsByTagName("AuthorLName1");
Element AuthorFName11Ele = (Element) AuthorFName11Node.item(0);
Log.i("AuthorLName1","AuthorLName1 - "+AuthorFName11Ele.getFirstChild().getNodeValue());
}
}
catch (Exception e) {
e.printStackTrace();
}
}