Monday, January 23, 2017

java xml parser - The DocumentBuilder

The DocumentBuilder stores its parsed result in a org.w3c.dom.Document object. You can call the Document's getDocumentelement() method to get the root node, and use the getChildNotes() method to get a list of the child nodes.

If you get compile-time warnings such as "warning: com.sun.org.apache.xerces.internal.parsers.DOMParser is Sun proprietary API and may be removed in a future release", you may consider using the SAXParser or the DocumentBuilder in the java.xml.parsers package.

import java.xml.parsers.DocumentBuilder;
import java.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class XMLParser {

    public void domParse(String xmlStr) {
        try {
            InputSource source = new InputSource(new StringReader(xmlStr));
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(source);
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            printNodeNameValue((Node) doc.getDocumentElement(), "Node name: ");
        } catch (ParserConfigurationException e) {
            e.printStackTrace(System.out);
        } catch (SAXException e) {
            e.printStackTrace(System.out);
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }
 
    private void printNodeNameValue(Node node, String nn){
        String nns = "  "+nn;
        String value = node.getNodeValue() == null ? "" : ("; node value: "+node.getNodeValue());
        System.out.println(nn+node.getNodeName()+value);
        if (node.hasChildNodes()){
            NodeList nodeList = node.getChildNodes();
            for(int i = 0; i < nodeList.getLength(); i++){
                printNodeNameValue(nodeList.item(i), nns);
            }
        }
    }
    public static void main(String[] args) {
        StringBuilder xml = new StringBuilder();
        xml.append("<store value='Deliciously Healthy'>")
                .append("<fruit>")
                .append("<name>Apple</name><price>1.68</price>")
                .append("</fruit><fruit>")
                .append("<name>Orange</name><price>0.89</price>")
                .append("</fruit>")
                .append("</store>");
        XMLParser xmlParser = new XMLParser();
        xmlParser.domParse(xml.toString());
    }
}

The output:
Root element :store
Node name: store
  Node name: fruit
    Node name: name
      Node name: #text; node value: Apple
    Node name: price
      Node name: #text; node value: 1.68
  Node name: fruit
    Node name: name
      Node name: #text; node value: Orange
    Node name: price
      Node name: #text; node value: 0.89

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

No comments:

Post a Comment