09 November 2016

XML DOM: Parsing XML Using Java DOM Level 2 NodeIterator & TreeWalker

Cấu trúc project
Hiển thị Console
student.xml
Java XML 2016
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students>
    <student>
        <id>std1</id>
        <name>Name 1</name>
        <age>20</age>
    </student>
    <student>
        <id>std2</id>
        <name>Name 2</name>
        <age>20</age>
    </student>
    
</students>
Run: Main.java
Java XML 2016
package dom;

/**
 *
 * @author Lonely
 */
public class Main {
    public static void main(String[] args) {
        //DemoNodeIterator dni = new DemoNodeIterator();
        //dni.Display();
       
        DemoTreeWalker dt = new DemoTreeWalker();
        dt.Display();
    }
}
DemoNodeIterator.java
Java XML 2016
package dom;

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

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

    public static void Display() {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            DOMImplementation dl = db.getDOMImplementation();
            if (!dl.hasFeature("traversal", "2.0")) {
                System.out.println("Don't suppot dom 2 - Không hõ trợ DOM level 2");
                return;
            }
            Document d = db.parse("src\\data\\student.xml");
            DocumentTraversal dt = (DocumentTraversal) d;
            NodeIterator ni = dt.createNodeIterator(d, NodeFilter.SHOW_ELEMENT, null, true);
            System.out.println("List Student");
            Node n;
            while ((n = ni.nextNode()) != null) {
                if (n.getNodeName().equalsIgnoreCase("id")) {
                    System.out.println("Id: " + n.getTextContent());
                }
                if (n.getNodeName().equalsIgnoreCase("name")) {
                    System.out.println("Name: " + n.getTextContent());
                }
                if (n.getNodeName().equalsIgnoreCase("age")) {
                    System.out.println("Age: " + n.getTextContent());
                }
                if (n.getNodeName().equalsIgnoreCase("student")) {
                    System.out.println("==================");
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
DemoTreeWalker.java
Java XML 2016
package dom;

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

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

    public void Display() {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document d = db.parse("src\\data\\student.xml");
            DocumentTraversal dt = (DocumentTraversal) d;
            TreeWalker tw = dt.createTreeWalker(d.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
            traverseNodes(tw);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void traverseNodes(TreeWalker tw) {
        Node n = tw.getCurrentNode();
        if (n.getNodeName().equalsIgnoreCase("id")) {
            System.out.println("Id: " + n.getTextContent());
        }
        if (n.getNodeName().equalsIgnoreCase("name")) {
            System.out.println("Name: " + n.getTextContent());
        }
        if (n.getNodeName().equalsIgnoreCase("age")) {
            System.out.println("Age: " + n.getTextContent());
        }
        if (n.getNodeName().equalsIgnoreCase("student")) {
            System.out.println("==================");
        }
        for (Node no = tw.firstChild(); no != null; no = tw.nextSibling()) {
            traverseNodes(tw);
        }
        tw.setCurrentNode(n);
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang