Dom4j XML parsing instance (2)

Source: Internet
Author: User

Dom4j is a Java xml api, similar to JDOM, used to read and write XML files. It features excellent performance, powerful functionality, and ease of use.

Jar packages used: dom4j-1.6.1.jar, jaxen-1.1-beta-6.jar

XML file to be parsed: people. xml

<people city="shenzhen">      <student name="milton" age="22"></student>      <student name="lego" age="23"></student>      <teacher name="bruce" age="27"></teacher>      <teacher name="ziven" age="28"></teacher>  </people>

 

The Java code is as follows:

package demo5;import java.io.File;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;  public class Test01 {    public static void main(String args[]) throws DocumentException {        SAXReader reader = new SAXReader();        Document document = reader.read(new File("D:/people.xml"));        Element rootElm = document.getRootElement();        //Element root1Elm = rootElm.element("city");        @SuppressWarnings("rawtypes")        List nodes = rootElm.elements("student");        @SuppressWarnings("rawtypes")List nodess = rootElm.elements("teacher");        for (@SuppressWarnings("rawtypes")        Iterator it = nodes.iterator(); it.hasNext();) {            Element elm = (Element) it.next();            System.out.println("name:" + elm.attributeValue("name")                    + " age:" + elm.attributeValue("age"));        }        for (@SuppressWarnings("rawtypes")        Iterator it = nodess.iterator(); it.hasNext();) {            Element elm = (Element) it.next();            System.out.println("name:" + elm.attributeValue("name")                    + " age:" + elm.attributeValue("age"));        }        System.out.println();        try {            Document doc = reader.read(new File("D:/people.xml"));            @SuppressWarnings("rawtypes")            List projects = doc.selectNodes("people/student");            @SuppressWarnings("rawtypes")List projectss = doc.selectNodes("people/teacher");            @SuppressWarnings("rawtypes")            Iterator it = projects.iterator();            while (it.hasNext()) {                Element elm = (Element) it.next();                System.out.println("name:" + elm.attributeValue("name")                        + " age:" + elm.attributeValue("age"));            }            @SuppressWarnings("rawtypes")            Iterator its = projectss.iterator();            while (its.hasNext()) {                Element elm = (Element) its.next();                System.out.println("name:" + elm.attributeValue("name")                        + " age:" + elm.attributeValue("age"));            }          } catch (Exception ex) {            ex.printStackTrace();        }    }  }

 

The result after the code is run is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.