dom4j读写xml实例

package com.huawei.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class CopyOfCopy {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
        try {
            XMLWriter writer = null;// 声明写XML的对象
            SAXReader reader = new SAXReader();

            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");// 设置XML文件的编码格式

            String filePath = "F:\\A.xml";
            String filePath2 = "F:\\B.xml";
            String abc = null;
            File file = new File(filePath);
            if (file.exists()) {
                Document document = reader.read(file);// 读取XML文件
                Element root = document.getRootElement();// 得到根节点
                boolean bl = false;
                for (Iterator i = root.elementIterator("string"); i.hasNext();) {
                    Element student = (Element) i.next();
                    if (student.attributeValue("name").equals("second")) {
                        // 修改学生sid=001的学生信息
//                        student.selectSingleNode("姓名").setText("王五");
//                        student.selectSingleNode("年龄").setText("25");

//                        writer = new XMLWriter(new FileWriter(filePath2), format);
//                        writer.write(document);
//                        writer.close();
                    abc=student.getText();
                    System.out.println(abc);
                        bl = true;
                        break;
                    }
                }
                if (bl) {
                    // 添加一个学生信息
                    Element student = root.addElement("string");
                    student.addAttribute("name", "second");
//                    Element sid = student.addElement("编号");
//                    sid.setText("100");
//                    Element name = student.addElement("姓名");
//                    name.setText("嘎嘎");
//                    Element sex = student.addElement("性别");
//                    sex.setText("男");
//                    Element age = student.addElement("年龄");
//                    age.setText("21");
                    student.setText(abc);
                    writer = new XMLWriter(new FileWriter(filePath2), format);
                    writer.write(document);
                    writer.close();
                }
            } else {
//                // 新建student.xml文件并新增内容
//                Document _document = DocumentHelper.createDocument();
//                Element _root = _document.addElement("学生信息");
//                Element _student = _root.addElement("学生");
//                _student.addAttribute("sid", "001");
//                Element _id = _student.addElement("编号");
//                _id.setText("001");
//                Element _name = _student.addElement("姓名");
//                _name.setText("灰机");
//                Element _age = _student.addElement("年龄");
//                _age.setText("18");
//
//                writer = new XMLWriter(new FileWriter(file), format);
//                writer.write(_document);
//                writer.close();
            }
            System.out.println("操作结束! ");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

你可能感兴趣的:(dom4j)