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

    这里写图片描述

    <?xml version="1.0" encoding="UTF-8"?>

    1. <!-- 手机的根节点 -->
    2.   <Phones>
      1.   <Brand name="三星">
      2.     <Type name="note4">note4</Type>
      3.     <Type name="note5">note5</Type>
      4.   </Brand>

     

      1.   <Brand name="苹果">
      2.       <Type name="iphone7">iphone7</Type>
      3.       <Type name="iphone8">iphone8</Type>
      4.           <Type name="iphone9">iphone9</Type>
      5.   </Brand>
    1.      </Phones>

    //这是XML文件


    //创建测试类
    package com.Denfeng.utils;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.Iterator;

    import org.dom4j.Document; //导这个包dom4j
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;

    //使用dom4j 解析xml文件,升级版,dom4j是对dom的封装 //重点

    public class ShowInfoUtils {

    public static void main(String[] args) {
        ShowInfoUtils s = new ShowInfoUtils();
        s.getDocument();
        s.showInfo();
    

    // s.delete();

    // s.update();
    // s.showWrite(“src/new2.xml”);

    // s.add();
    // s.showWrite(“src/new4.xml”);

    // s.showWrite(“src/new1.xml”);
    // s.showInfo();
    }

    public Document doc;
    
    //加载doc对象
    //获得doc对象
    public void getDocument(){
        try {
            SAXReader sr = new SAXReader();
            doc=sr.read(new File("src/phone.xml"));
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //删除一个节点
    public void delete(){
        Element e = doc.getRootElement();   //得到根节点
        Iterator iter = e.elementIterator();        //拿到迭代器
        //对迭代器进行遍历
        while(iter.hasNext()){ 
            Element ele=(Element)iter.next();       //返回的是object,要强转
            if(ele.attributeValue("name").equals("三星")){
                ele.getParent().remove(ele);
            }
        }
    
    
    }
    
    //删除之后 开始写入xml文件中
    public void showWrite(String path){
    
        //格式化类,设置编码格式
        try {
            OutputFormat outputFormat = OutputFormat.createCompactFormat();
             outputFormat.setEncoding("utf-8");
    
             //dom4j给我们提供了一个写入的类
            XMLWriter xWriter = new XMLWriter(new FileOutputStream(path),outputFormat);
            xWriter.write(doc);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //修改 ,加一个id
    public void update(){
        //获得根节点
        Element e = doc.getRootElement();
        Iterator iterator =e.elementIterator();
        int num=0;
        while(iterator.hasNext()){
            Element element = (Element)iterator.next();
            num++;
            element.setAttributeValue("id", num+"");
    
        }
    
    }
    
    //增加一个Brand
    public void add(){
        Element e = doc.getRootElement();
    

    // Iterator iter = e.elementIterator();
    Element elementBrand = e.addElement(“Brand”);
    elementBrand.setAttributeValue(“name”, “华为”);
    Element elementType = elementBrand.addElement(“Type”);
    elementType.setAttributeValue(“name”, “galax”);
    e.appendContent(elementBrand);
    }

    //展示XML信息
    public void showInfo(){
        //得到根节点,也就是XML里的Phones节点
        Element e = doc.getRootElement();
    
        //得到所有的brand对象,然后进行遍历
    
        Iterator iterator = e.elementIterator();        //拿到迭代器
        //判断是否有下一个元素
        while(iterator.hasNext()){
            //拿到具体的元素
            Element elementBrand = (Element)iterator.next();                //此时找到了brand标签,
            String elementBranValue = elementBrand.attributeValue("name");  //找到name属性对应的值
            System.out.println(elementBranValue);                           //输出三星  苹果
    
            //得到儿子的节点
            Iterator elementIterator = elementBrand.elementIterator();      //拿到Brand迭代器
            while(elementIterator.hasNext()){
                Element ele= (Element)elementIterator.next();               //得到每一个子标签
                 String s = ele.attributeValue("name");
                 System.out.println(s);
            }
        }
    
    }
    

    }

  • 相关阅读:
    让你的App飞一会
    Event in Backbone
    Event in Zepto
    【不怕坑】之 Node.js加密 C#解密
    结对项目:日程管理(四)
    结对项目:日程管理(三)
    结对项目:日程管理(二)
    结对项目:日程管理(一)
    当代大学生的痛点
    软件需求分析
  • 原文地址:https://www.cnblogs.com/binghuaZhang/p/10719522.html
Copyright © 2011-2022 走看看