Java DOM结合XPath快速解析XML文件

如果要定位某个XML文档中的一段特性信息,那么通过遍历DOM树的众多节点来进行查找显得有些麻烦。XPath是的访问树节点变得很容易。

需要先了解Java XML解析,可以参考Java验证解析XML

场景

<bookstore>  
    <book id="book1">  
        <name>Java 核心技术</name>  
        <author>Cornell </author>  
        <year>2014</year>  
        <price>89</price>  
    </book>  
    <book id="book2">  
        <name>深入浅出MyBatis</name>  
        <author>杨开振</author>  
        <year>2016</year>  
        <price>69</price>  
    </book>  
    <book id="book3">  
        <name>Java RESTful Web Service实战</name>  
        <author>韩陆</author>  
        <year>2016</year>  
        <price>59</price>
    </book>  
</bookstore>  

如果需要得到author节点,可以通过XPath表达式/bookstore/book/author来得到所有的author节点。java代码如下。

/**
* @author gethin
* @version 创建时间:2018年4月8日 下午3:17:04
* 类说明
*/
public class ReadXMLByDOMWithXpath {
    private static DocumentBuilderFactory dBuilderFactory = null;
    private static DocumentBuilder dBuilder = null;
    private static XPathFactory xPathFactory = null;
    private static XPath xPath = null;
    static {
        try {
            /**
             * 要读入一个XML文档,首先要有一个DocumentBuilder对象 可以从DocumentBuilderFactory中得到这个对象
             */
            dBuilderFactory = DocumentBuilderFactory.newInstance();
            //设置验证
            dBuilderFactory.setValidating(true);
            //忽略空白字符节点
            dBuilderFactory.setIgnoringElementContentWhitespace(true);
            dBuilder = dBuilderFactory.newDocumentBuilder();
            xPathFactory = XPathFactory.newInstance();
            xPath = xPathFactory.newXPath();
            dBuilder.setErrorHandler(new ErrorHandler() {

                public void warning(SAXParseException exception) throws SAXException {
                    throw exception;

                }

                public void fatalError(SAXParseException exception) throws SAXException {
                    // TODO Auto-generated method stub
                    throw exception;

                }

                public void error(SAXParseException exception) throws SAXException {
                    // TODO Auto-generated method stub
                    throw exception;
                }
            });
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //获得所有的author节点并输出
    public static void showAuthorByXPath(String fileName) throws SAXException, IOException, XPathExpressionException {
        Document document = dBuilder.parse(fileName);
        NodeList nodeList = (NodeList) xPath.evaluate("/bookstore/book/author", document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Element author = (Element) nodeList.item(i);
            System.out.println(author.getTextContent().trim());
        }
    }

    public static List<Book> listBooksWithXpath(String fileName)
            throws XPathExpressionException, SAXException, IOException {
        List<Book> books = new ArrayList<Book>();
        // 可通过DocumentBuilder对象的parse()方法读入整个文档
        Document document = dBuilder.parse(fileName);
        // 获得所有book节点
        NodeList nodeList = (NodeList) xPath.evaluate("/bookstore/book", document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Element bookElement = (Element) nodeList.item(i);
            // 获得该book节点下的所有属性节点
            NodeList bookAttribute = bookElement.getChildNodes();
            // 用来存储第i个节点的内容
            List<String> bookContent = new ArrayList<String>();
            Book book = new Book();
            book.setId(Integer.parseInt(bookElement.getAttribute("id").replace("book", "").trim()));
            for (int j = 0; j < bookAttribute.getLength(); j++) {
                Element atturbute = (Element) bookAttribute.item(j);
                bookContent.add(atturbute.getTextContent().trim());
            }
            book.setName(bookContent.get(0));
            book.setAuthor(bookContent.get(1));
            book.setYear(Integer.parseInt(bookContent.get(2)));
            book.setPrice(Integer.parseInt(bookContent.get(3)));
            books.add(book);
        }
        return books;
    }

    public static void main(String args[]) {
        String fileName = "./src/main/java/com/gethin/xmlparser/bookstore.xml";
        try {
             List<Book> books = ReadXMLByDOMWithXpath.listBooksWithXpath(fileName);
             for (Book book : books) {
             System.out.println(book);
             }
            showAuthorByXPath(fileName);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行截图:


DOM结合XPath解析XML文档

请参考,源码github链接

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【引言】 目前在Java中用于解析XML的技术很多,主流的有DOM、SAX、JDOM、DOM4j,下文主要介绍这4...
    低至一折起阅读 1,115评论 0 7
  • ···lxml用法源自 lxml python 官方文档,更多内容请直接参阅官方文档,本文对其进行翻译与整理。lx...
    小丰丰_72a2阅读 1,015评论 0 1
  • 非本人所写,在学习的时候觉得写的挺详细的。分享一下。 XML文件是一种常用的文件格式,例如WinForm里面的ap...
    毕竟是秀秀啊阅读 2,705评论 0 2
  • 三年前的春节,我完成了一件很重要的事情,那就是尝试用一天的时间读完一本书。 我读的第一本就是奥巴马的传记,差不多有...
    宇枫Sai阅读 1,186评论 1 0
  • 今天天气晴朗,冬日里暖日怒放,银杏叶越加发黄的美丽! 感觉有些沮丧,后悔之前不知道要进步,现在感觉牛人太多,自己做...
    林溪amanda阅读 115评论 0 0