Back to project page AZBible.
The source code is released under:
GNU General Public License
If you think the Android project AZBible listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.witgeeks.azbible.xml; /* w w w.ja v a 2 s . co m*/ import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import android.util.Log; public class XMLDOMParser { //Returns the entire XML document public Document getDocument(InputStream inputStream) { Document document = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(inputStream); document = db.parse(inputSource); } 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 document; } /* * I take a XML element and the tag name, look for the tag and get * the text content i.e for <employee><name>Kumar</name></employee> * XML snippet if the Element points to employee node and tagName * is name I will return Kumar. Calls the private method * getTextNodeValue(node) which returns the text value, say in our * example Kumar. */ public String getValue(Element item, String name) { NodeList nodes = item.getElementsByTagName(name); return this.getTextNodeValue(nodes.item(0)); } private final String getTextNodeValue(Node node) { Node child; if (node != null) { if (node.hasChildNodes()) { child = node.getFirstChild(); while(child != null) { if (child.getNodeType() == Node.TEXT_NODE) { return child.getNodeValue(); } child = child.getNextSibling(); } } } return ""; } }