dom4j解析xml文件

本文介绍使用DOM4J高效解析XML文件的方法。通过具体示例展示了如何读取XML结构,包括获取根节点名称、元素属性及文本内容,并遍历特定节点。此外还提供了插入新元素并保存到文件的操作示范。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   xml文件的解析有很多方法,其中dom4j是效率相对比较高的,现整理如下:

     package parse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


public class Dom4JParse {
   
   
    private static final String filePath="jdom.xml";
    private static final String classPath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
    private static SAXReader reader;
    private static Document document;
   
   
    static{
       
        reader=new SAXReader();
        try {
            File file=new File(classPath+filePath);
            Reader r=new InputStreamReader(new FileInputStream(file),"UTF-8");
            System.out.println("file --Path---"+file.getPath());
            document=reader.read(r);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
    }
   
   
   
    public void parse(){
       
        Element rootEle=document.getRootElement();
        String rootText=rootEle.getName();
        System.out.println("root---Name"+rootText);
       
        Element areaEle=rootEle.element("area");
        System.out.println("名字----"+areaEle.getName()+"  文本------"+areaEle.getText());
       
        List provinceList=rootEle.elements("province");
       
        for (int i = 0; i < provinceList.size(); i++) {
            Element province=(Element) provinceList.get(i);
            String proText=province.attributeValue("text");
            System.out.println("proText----"+proText);
            List cityList=province.elements("city");
           
           
           
           
            for (int j = 0; j < cityList.size(); j++) {
                Element city=(Element) cityList.get(j);
                String cityText=city.getText();
                System.out.println("cityText---"+cityText);
            }
        }
       
       
        //把document以xml形式打印出来
        System.out.println(document.asXML());
       
       
    }
   
   
    public void insert(){
       
        Element rootEle=document.getRootElement();
        Element population=rootEle.addElement("population");
        population.setText("130000");

       
        try {
            XMLWriter writer=new XMLWriter(new FileWriter(classPath+filePath));
            writer.write(document);
            //writer.
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
       
       
       
       
    }
   
   
    public static void main(String[] args) {
       
        new Dom4JParse().parse();
        //new Dom4JParse().insert();
        //System.out.println("我日");
    }
   
   

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值