Domj4读取xml文件步骤及方法

使用步骤:
1)导入dom4j的核心包。 dom4j-1.6.1.jar
2)编写Dom4j读取xml文件代码**

    public static void main(String[] args) {
        try {
            //1.创建一个xml解析器对象
            SAXReader reader = new SAXReader();
            //2.读取xml文档,返回Document对象
            Document doc = reader.read(new File("./src/contact.xml"));

            System.out.println(doc);
        } catch (DocumentException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

Domj4读取xml文件

节点:(Node)
    Iterator  Element.nodeIterator();  //获取当前标签节点下的所有子节点
标签:(Element)
    Element  Document.getRootElement();  //获取xml文档的根标签      
    Element   ELement.element("标签名") //指定名称的第一个子标签
    Iterator<Element> Element.elementIterator("标签名");// 指定名称的所有子标签
    List<Element>    Element.elements(); //获取所有子标签      属性:(Attribute)
    String   Element.attributeValue("属性名") //获取指定名称的属性值
    Attribute    Element.attribute("属性名");//获取指定名称的属性对象 
    Attribute.getName()  //获取属性名称
    Attibute.getValue()  //获取属性值
    List<Attribute>  Element.attributes();  //获取所有属性对象
    Iterator<Attribute>     Element.attibuteIterator(); //获取所有属性对象
文本:(Text)
    Element.getText();  //获取当前标签的文本
    Element.elementText("标签名") //获取当前标签的指定名称的子标签的文本内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值