xml既简单又标准,值得拥有和学习,好多地方都用的到。
假设有这么一个book.xml:




-
-
Harry Potter
J K. Rowling
2005
29.99

-
Everyday Italian
Giada De Laurentiis
2005
30.00

-
Learning XML
Erik T. Ray
2003
39.95

-
XQuery Kick Start
James McGovern
Per Bothner
Kurt Cagle
James Linn
Vaidyanathan Nagarajan
2003
49.99

一、w3c.DOM的方式读取

1、代码:

/**
     * java读取xml文件四中方式之一
     * w3c.DOM方式实现
     * Red_ant
     * 20181105
     */ 
    public static Document useDomReadXml(String soucePath){
        File file = new File(soucePath);
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(file);
            return doc;
        } catch (Exception e) {
            System.err.println("读取该xml文件失败");
            e.printStackTrace();
        }
        return null;
    }

2、测试用例:

    /**
     * java以w3c.DOM方式读取xml文件        
     */
    String soucePath = "E:\\AllFilesISHere\\testfile\\books.xml";
    org.w3c.dom.Document doc = AllServiceIsHere.useDomReadXml(soucePath);
    //读取xml内部节点集合
    org.w3c.dom.NodeList nlst = doc.getElementsByTagName("book");
    //遍历集合内容
    for (int i = 0; i < ((org.w3c.dom.NodeList) nlst).getLength(); i++) {
        String title = doc.getElementsByTagName("title").item(i).getFirstChild().getNodeValue();
        String creater = doc.getElementsByTagName("author").item(i).getFirstChild().getNodeValue();         
        String year = doc.getElementsByTagName("year").item(i).getFirstChild().getNodeValue();
        String price = doc.getElementsByTagName("price").item(i).getFirstChild().getNodeValue();
        System.err.println("标题"+ title);
        System.err.println("作者"+creater);
        System.err.println("年份"+ year);
        System.err.println("价格"+ price);
    }

3、演示结果:

java读取xml文件的三种方法_第1张图片

二、Java以DOM4J的方式读取xml文件

1、代码:

/**
     * java读取xml的四种方法之二
     * DOM4J方式实现
     */
    public static Element useDom4JReadXml(String soucePath){
        try {
            File file = new File(soucePath);
            SAXReader read = new SAXReader();
            org.dom4j.Document doc = read.read(file);
            Element root = doc.getRootElement();
            return root;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

2、测试:

/**
         * java以DOM4J的方式读取xml
         */
    org.dom4j.Element ele = AllServiceIsHere.useDom4JReadXml(soucePath);
        org.dom4j.Element foo ;
        for (Iterator i = ele.elementIterator("book"); i.hasNext();) {
            foo = (org.dom4j.Element) i.next();
            String title = foo.elementText("title");
            String creater = foo.elementText("author");
            String year = foo.elementText("year");
            String price = foo.elementText("price");
            System.err.println("标题"+ title);
            System.err.println("作者"+creater);
            System.err.println("年份"+ year);
            System.err.println("价格"+ price);
        }

3、演示:

java读取xml文件的三种方法_第2张图片

三、java以JDOM的方式实现读取xml文件

1、代码:

/**java读取xml的四种方法之三
     * 以JDOM的方式实现读取xml文件
     */
    public static org.jdom.Element useJDOMReadXml(String soucePath){
        try {
            SAXBuilder builder = new SAXBuilder();
            org.jdom.Document doc = builder.build(new File(soucePath));
            org.jdom.Element foo = doc.getRootElement();
            return foo;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

2、测试:

/**
     * java以JDOM的方式读取xml
     */
org.jdom.Element foo = AllServiceIsHere.useJDOMReadXml(soucePath);
        @SuppressWarnings("unchecked")
        List chilLst = foo.getChildren();
        for (int i = 0; i < chilLst.size(); i++) {
            String title = ((org.jdom.Element) chilLst.get(i)).getChild("title").getText();
            String creater = ((org.jdom.Element) chilLst.get(i)).getChild("author").getText();
            String year = ((org.jdom.Element) chilLst.get(i)).getChild("year").getText();
            String price = ((org.jdom.Element) chilLst.get(i)).getChild("price").getText();
            System.err.println("标题"+ title);
            System.err.println("作者"+creater);
            System.err.println("年份"+ year);
            System.err.println("价格"+ price);
            System.err.println("**************--------------********");
        }

3、演示:

java读取xml文件的三种方法_第3张图片