15 November 2016

XML DOM: Đọc toàn bộ cấu trúc file XML (Hiển thị node gốc, các node con, value, attribute)

Question_2
Document_DOM.java
Java XML 2016
package controller;

import javax.xml.parsers.*;
import org.w3c.dom.Document;

/**
 *
 * @author Lonely
 */
public class Document_DOM {

    public static Document getDocumentNew() {
        Document d = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            d = db.newDocument();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return d;
    }

    public static Document getDocumentParse(String path_to_file) {
        Document d = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            d = db.parse(path_to_file);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return d;
    }
}
 

ReadDOM.java
Java XML 2016
package question_2;

import static controller.Document_DOM.getDocumentParse;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Lonely
 */
public class ReadDOM {

    public void Display() {
        try {
            NodeList nList = getDocumentParse("src\\Data\\products.xml").getElementsByTagName("product");

            System.out.println("===QUESTION [2] Reading DOM");

            for (int i = 0; i < nList.getLength(); i++) {

                Node nNode = nList.item(i);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    //1. Node goc
                    System.out.println("\n[1]. Node goc");
                    System.out.println("Node name: "+nNode.getNodeName());
//                    System.out.println("Node content: "+nNode.getTextContent());

                    //2. Node con
                    System.out.println("\n[2]. Node con");
                    if (nNode.hasChildNodes()) {
                        nodeCon(nNode.getChildNodes());
                    }

                    //3. Value
                    Element el = (Element) nList.item(i);
                    NamedNodeMap nnm = el.getAttributes();
                    if (nnm.getLength() > 0) {
                        System.out.println("\n[3]. Value");
                        System.out.println("Name: " + el.getElementsByTagName("name").item(0).getTextContent());
                        System.out.println("Des: " + el.getElementsByTagName("des").item(0).getTextContent());
                        System.out.println("Price: " + el.getElementsByTagName("price").item(0).getTextContent());
                        System.out.println("Catalog: " + el.getElementsByTagName("catalog").item(0).getTextContent());
                    }

                    //4. Attribute
                    if (nNode.hasAttributes()) {
                        System.out.println("\n[4]. Attribute");
                        NamedNodeMap nMap = nNode.getAttributes();
                        for (int j = 0; j < nMap.getLength(); j++) {
                            Node node = nMap.item(j);
                            System.out.println("Attr name : " + node.getNodeName());
                            System.out.println("Attr value : " + node.getNodeValue());
                        }
                    }
                }

            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void nodeCon(NodeList nList) {
        for (int i = 0; i < nList.getLength(); i++) {
            Node nNode = nList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println("Node name =" + nNode.getNodeName());
//                System.out.println("Node content =" + nNode.getTextContent());
            }
        }
    }

}
Run test: Main.java
Java XML 2016
package run;

import question_1.CreateXML;
import question_2.ReadDOM;
/**
 *
 * @author Lonely
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //1 Tạo file XML dùng DOM 
        CreateXML c = new CreateXML();
        c.create();

        //2 Đọc toàn bộ cấu trúc file XML (Hiển thị node gốc, các node con, value, attribute)
        ReadDOM r = new ReadDOM();
        r.Display();
    }
}
Run test

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang