dom4j读XML

import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class ReadXMLByDom {
    public static void main(String [] args){
        ReadXMLByDom r = new ReadXMLByDom();
        r.readXMLAllAttr();
        r.readXMLAllElement();
        r.readXMLAttr();
        r.readXMLElement();
    }
   
    //第一种:根据属性来查找
    //结果为:<book id="2"><Name>Linux</Name><price>100</price></book>
    public void readXMLAttr() {
        String str = "<books><book id='1'><Name>Java</Name><price>100</price></book>";
        str = str
                + "<book id='2'><Name>Linux</Name><price>100</price></book></books>";
        // 生成一个Document
        Document document = null;
        try {
            document = DocumentHelper.parseText(str);
            Element book = (Element) document
                    .selectSingleNode("/books/book[@id='2']");
            System.out.println(book.asXML());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    //第二种:根据节点名来查找
    //结果为:Java,Linux,
    public void readXMLElement() {
        String str = "<books><book id='1'><Name>Java</Name><price>100</price></book>";
        str = str
                + "<book id='2'><Name>Linux</Name><price>100</price></book></books>";
        Document document = null;
        try {
            document = DocumentHelper.parseText(str);
            List names = document.selectNodes("//Name");// 记得这边是双斜杠且有区分大小写
            for (int i = 0; i < names.size(); i++) {
                System.out.print(((Element) names.get(i)).getText() + ",");
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    //第三种:遍历整个XML取得属于指定类型节点的记录
    //结果为:1 Java 100
    //       2 Linux 100
    public void readXMLAllElement() {
        String str = "<books><book id='1'><Name>Java</Name><price>100</price></book>";
        str = str
                + "<book id='2'><Name>Linux</Name><price>100</price></book></books>";
        Document document = null;
        try {
            document = DocumentHelper.parseText(str);
            Iterator it = document.getRootElement().elementIterator();
            Element element = null;
            while (it.hasNext()) {
                element = (Element) it.next();
                if (element.getName().equals("book")) {
                    System.out.print(element.attributeValue("id") + " ");
                    System.out.print(element.element("Name").getText() + " ");
                    System.out.println(element.element("price").getText());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    //第四种:遍历所有属性
    //结果为:id:1
    //       name:Java
    //       price:120
    public void readXMLAllAttr() {
        String str = "<book id='1' name='Java' price='120'/>";
        Document document;
        try {
            document = DocumentHelper.parseText(str);
            Iterator attributes = document.getRootElement().attributeIterator();
            while (attributes.hasNext()) {
                Attribute attribute = (Attribute) attributes.next();
                System.out.println(attribute.getName() + ":"
                        + attribute.getValue());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qking93415981/archive/2007/09/14/1785114.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值