XPath를 통한 Java 구문 분석 xml 노드의 자세한 코드 설명

Y2J
풀어 주다: 2017-04-27 09:24:54
원래의
2836명이 탐색했습니다.

import java.io.File;
import java.io.FileInputStream;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
public class FindElementsByAbsoluteLocationWithXPath {
 
    public static void main(String[] args) throws Exception {
 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
 
        Document doc = db.parse(new FileInputStream(new File("in.xml")));
 
        XPathFactory factory = XPathFactory.newInstance();
 
        XPath xpath = factory.newXPath();
 
        String expression;
        Node node;
        NodeList nodeList;
 
        // 1. root element
        expression = "/*";
        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
        System.out.println("1. " + node.getNodeName());
 
        // 2. root element (by name)
        expression = "/rss";
        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
        System.out.println("2. " + node.getNodeName());
 
        // 3. element under rss
        expression = "/rss/channel";
        node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
        System.out.println("3. " + node.getNodeName());
 
        // 4. all elements under rss/channel
        expression = "/rss/channel/*";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("4. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();
 
        // 5. all title elements in the document
        expression = "//title";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("5. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();
 
        // 6. all elements in the document except title
        expression = "//*[name() != &#39;title&#39;]";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("6. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();
 
        // 7. all elements with at least one child element
        expression = "//*[*]";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("7. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();
 
        // 8. all level-5 elements (the root being at level 1)
        expression = "/*/*/*/*";
        nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
        System.out.print("8. ");
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName() + " ");
        }
        System.out.println();
 
    }
 
}
로그인 후 복사

입력:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>Java Tutorials and Examples 2</title>
        <language>en-us</language>
        <item>
            <title><![CDATA[Java Tutorials 2]]></title>
            <link>http://www.javacodegeeks.com/</link>
        </item>
        <item>
            <title><![CDATA[Java Examples 2]]></title>
            <link>http://examples.javacodegeeks.com/</link>
        </item>
    </channel>
</rss>
로그인 후 복사

출력:

1. rss
2. rss
3. channel
4. title language item item
5. title title title
6. rss channel language item link item link
7. rss channel item item
8. title link title link
로그인 후 복사

위 내용은 XPath를 통한 Java 구문 분석 xml 노드의 자세한 코드 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!