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

2018-04-17  本文已影响0人  d3f59bfc7013

如果要定位某个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链接

上一篇 下一篇

猜你喜欢

热点阅读