Java dom4j解析XML文件

1.分析步骤:

  • 一、获取读取文档的对象SaxReader。
  • 二、public Document read(File file) : 将XML文档读取到内存中,生成Document对象。
  • 三、public Element getRootElement() : 获取根标签元素对象。
  • 四、public List elements(String elementName) :获取指定名称的子标签元素对象,并将象封装到List集合中。
  • 五、遍历集合,获取子标签内元素。
  • 六、public Element element(String elementName) : 获取指定名称的子标签元素对象。
    -七、 public String getText() : 获取Element标签体中的内容作为字符串返回。
  • 八、(6、7合并(获取子标签文本内容)):
    两步:1.获取到指定名称的子标签对象 2.获取到子标签对象的文本内容。
  • 九、public Arrtibute attribute(String attributeName) :
    获取指定名称的属性对象,返回Arrtibute对象。
  • 十、public String getValue() : 获取属性对象的属性值,作为字符串返回。
  • 十一、(9、10合并):
    public Arrtibute attribute(String attributeName) : 获取指定名称的属性对象,返回Arrtibute对象.
    public Arrtibute attributeValue(String attributeName) :找到指定名称的属性对象 2.获取到属性对象的属性值内容。

2.XML文件

在这里插入图片描述

3.代码:

public class TextXml {
    public static void main(String[] args) throws DocumentException {
        // 1.获取读取文档的对象SaxReader.
        SAXReader saxReader = new SAXReader();

        // 2.public Document read(File file) : 将XML文档读取到内存中,生成Document对象.
        Document document = saxReader.read(new File("day15_XML_meiju\\Students.xml"));

        // 3.public Element getRootElement() : 获取根标签元素对象.
        Element rootElement = document.getRootElement();


        // 4.public List<Element> elements(String elementName) :获取指定名称的子标签元素对象,并将象封装到List集合中.

        List<Element> element = rootElement.elements("student");

        // 5.遍历集合,获取子标签内元素
        for (Element elements : element) {

            // 6.public Element element(String elementName) : 获取指定名称的子标签元素对象.
            Element nameElement = elements.element("name");

            // 7.public String getText() : 获取Element标签体中的内容作为字符串返回.
            String name = nameElement.getText();
        /*
            8.  6、7合并(获取子标签文本内容):
            两步:1.获取到指定名称的子标签对象 2.获取到子标签对象的文本内容
            
         */
            String age = elements.elementText("age");
            String address = elements.elementText("address");

            //  9.public Arrtibute attribute(String attributeName) : 获取指定名称的属性对象,返回Arrtibute对象.
            Attribute attribute = elements.attribute("ID");

            //  10.public String getValue() : 获取属性对象的属性值,作为字符串返回.
            String id = attribute.getValue();

       /*
            11. 9、10合并:
             public Arrtibute attribute(String attributeName) : 获取指定名称的属性对象,返回Arrtibute对象.
             public Arrtibute attributeValue(String attributeName) :找到指定名称的属性对象 2.获取到属性对象的属性值内容
        */

            String s = elements.attributeValue("sex");

            System.out.println("学号为:" + id + ",姓名是:" + name + ",年龄是:" + age + ",性别为:" + s + ",地址是" + address);
        }
    }
}

4.运行结果:

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多喝清晨的粥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值