Android Open Source - AZBible X M L D O M Parser






From Project

Back to project page AZBible.

License

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.

Java Source Code

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 "";
    }
}




Java Source Code List

com.witgeeks.azbible.BooksActivity.java
com.witgeeks.azbible.ChaptersActivity.java
com.witgeeks.azbible.LessonActivity.java
com.witgeeks.azbible.MainActivity.java
com.witgeeks.azbible.beans.Bible.java
com.witgeeks.azbible.beans.Book.java
com.witgeeks.azbible.beans.Chapter.java
com.witgeeks.azbible.beans.Verse.java
com.witgeeks.azbible.sqlite.DataBaseManager.java
com.witgeeks.azbible.sqlite.DbHelper.java
com.witgeeks.azbible.xml.XMLDOMParser.java