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);
        }
    }

}

}

转载于:https://www.cnblogs.com/binghuaZhang/p/10719522.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值