zoukankan      html  css  js  c++  java
  • dom4j解析XML文件


    public class TestDom4j {
      Document doc = null;

      //初始化,给doc指定路径
      public TestDom4j() {
        SAXReader reader = new SAXReader();
        try {
          doc = reader.read("src/PhoneInfo.xml");
        } catch (DocumentException e) {
          e.printStackTrace();
        }
      }

      //遍历doc中的元素
      public void show() {
        //1.获取根节点
        Element root = doc.getRootElement();
        //2.获取根节点下的分支集合
        List<Element> brands = root.elements();
        //3.遍历根节点下的分支
        for(Element brand : brands) {
          System.out.println(brand.getName() + ":" + brand.attributeValue("name"));
          List<Element> types = brand.elements();
          for(Element type : types) {
            System.out.println(" " + type.getName() + ":" + type.attributeValue("name"));
          }
        }
      }

      //保存XML文件
      public void save(String path) {
        try {
          //1.创建写入器,并指定保存路径
          XMLWriter writer = new XMLWriter(new FileOutputStream(path), OutputFormat.createPrettyPrint());
          //2.从指定文件读取内容
          writer.write(doc);
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      //在brand栏中添加id属性
      public void addAtt() {
        //1.获取根节点
        Element root = doc.getRootElement();
        List<Element> brands = root.elements();
        int i = 1;
        for(Element brand : brands) {
          brand.addAttribute("id", i + "");
          i++;
        }
        save("src/new_PhoneInfo.xml");
      }

      //增加节点
      public void addNodes() {
        //1.获取根节点
        Element root = doc.getRootElement();
        //2.在根节点下创建分支
        Element addBrand = root.addElement("Brand");
        //3.在新建的分支中增加属性
        addBrand.addAttribute("name", "iphone");
        //4.在新建的分支下再创建分支
        Element addType = addBrand.addElement("Type");
        addType.addAttribute("name", "iphone7");
        save("src/new_PhoneInfo.xml");
      }

      //删除节点
      public void delete() {
        //1.获取根节点
        Element root = doc.getRootElement();
        List<Element> brands = root.elements();
        //2.遍历根节点下的分支
        for(Element brand : brands) {
          //3.获取分支中的属性值
          if("华为".equals(brand.attributeValue("name"))) {
            root.remove(brand);
          }
        }
        save("src/new_PhoneInfo.xml");
      }

      public static void main(String [] args) {
        TestDom4j td = new TestDom4j();
        //td.show();
        //td.save();
        //td.addAtt();
        //td.addNodes();
        td.delete();
      }
    }

    附上XML文件对应的内容

    <?xml version="1.0" encoding="UTF-8"?>
    <PhoneInfo>
      <Brand name = "华为">
        <Type name = "Mate 10"></Type>
        <Type name = "Honor V10"></Type>
        <Type name = "畅玩7X"></Type>
      </Brand>
      <Brand name = "小米">
        <Type name = "小米6"></Type>
        <Type name = "小米5 Plus"></Type>
        <Type name = "MIX2"></Type>
        <Type name = "小米Note3"></Type>
      </Brand>
    </PhoneInfo>

  • 相关阅读:
    最长公共前缀
    罗马数字转整数
    回文数
    整数反转
    Linux内核设计与实现——进程管理
    技术派-常用的一些VS相关的宏名
    假如面试3道小学数学题,你可否会?
    技术派-不用sqrt手工计算平方根
    观察者-学历差距造成的差距有多大
    10G文件如何对里面单词出现排序
  • 原文地址:https://www.cnblogs.com/helloworldlx/p/8577068.html
Copyright © 2011-2022 走看看