码迷,mamicode.com
首页 > 编程语言 > 详细

Java从零开始学四十三(DOM4j解析XML)

时间:2015-04-07 01:53:07      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

一、创建XML

// 建立XML
    public static void gernatorXML() {
        // 创建Document对象
        Document doc = DocumentHelper.createDocument();
        // 创建根节点students
        Element studentsElement = doc.addElement("students");
        // 创建子节点student
        Element studentElement1 = studentsElement.addElement("student");
        // 添加ID属性
        studentElement1.addAttribute("id", "1001");
        // 添加子节点
        studentElement1.addElement("name").setText("张三");
        studentElement1.addElement("age").setText("18");
        studentElement1.addElement("sex").setText("男");
        studentElement1.addElement("phone").setText("13800138000");
        studentElement1.addElement("address").setText("深圳宝安");

        // 再创建同样的2个student节点
        Element studentElement2 = studentsElement.addElement("student");
        // 添加ID属性
        studentElement2.addAttribute("id", "10012");
        // 添加子节点
        studentElement2.addElement("name").setText("李四");
        studentElement2.addElement("age").setText("19");
        studentElement2.addElement("sex").setText("女");
        studentElement2.addElement("phone").setText("13412369854");
        studentElement2.addElement("address").setText("上海");
        // 再创建同样的2个student节点
        Element studentElement3 = studentsElement.addElement("student");
        // 添加ID属性
        studentElement3.addAttribute("id", "10013");
        // 添加子节点
        studentElement3.addElement("name").setText("王五");
        studentElement3.addElement("age").setText("25");
        studentElement3.addElement("sex").setText("女");
        studentElement3.addElement("phone").setText("13410110104");
        studentElement3.addElement("address").setText("广东");

        try {
            // 创建XML 将Document写入XML
            // 设置格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            // 设置字符编码也可以设置为gb2312或者gbk
            format.setEncoding("utf-8");
            // 输入
            XMLWriter writer = new XMLWriter(
                    new FileWriter("xml/students.xml"), format);

            // 写入文档
            writer.write(doc);
            // 关闭流
            writer.close();
            // 输出提示
            System.out.println("======写入成功=======");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

二、解析XML

    // 读取XML
    public static void getXML() {

        try {
            // 读取XML
            SAXReader reader = new SAXReader();
            Document doc = reader.read("xml/students.xml");
            // 获取根节点
            Element root = doc.getRootElement();
            // 获取子节点集合
            List<Element> stulList = root.elements();
            // 遍历
            for (Element ele : stulList) {
                // 获取属性
                String id = ele.attributeValue("id");
                System.out.println("学生ID: " + id);
                // 获取子节点的文本内容 name
                String name = ele.elementText("name");
                // age年龄
                String age = ele.elementText("age");
                // sex
                String sex = ele.elementTextTrim("sex");
                // phone
                String phone = ele.elementTextTrim("phone");
                // address
                String address = ele.elementTextTrim("address");
                System.out.println("姓名:" + name + "\t年龄:  " + age + "\t 性别:  "
                        + sex + "\t\t电话:" + phone + "\t\t地址:  " + address);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

 

三、增加节点

//增加新的节点
    public static void addXML(){
        
        try {
            //读取XML创建解析器
            SAXReader reader=new SAXReader();
            //创建Document对象
            Document doc=reader.read("xml/students.xml");
            //获取根节点
            Element root=doc.getRootElement();
            //创建新的节点
            Element student=root.addElement("student");
            //添加属性
            student.addAttribute("id", "10020");
            //创建子节点
            student.addElement("name").setText("张三丰");
            student.addElement("age").setText("108");
            student.addElement("sex").setText("男");
            student.addElement("phone").setText("13800138001");
            student.addElement("address").setText("武当山");
            //写入XML
            //设置字符编码和格式
            OutputFormat format=new OutputFormat();
            format.setEncoding("utf-8");
            //创建输出流
            XMLWriter writer=new XMLWriter(new FileWriter("xml/students.xml"), format);
            //写入Document
            writer.write(doc);
            //关闭流
            writer.close();
            //给出提示
            System.out.println("======增加成功=======");
            
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

 

四、修改节点

//修改节点内容根据 id10013将王五的姓名=王小五,年龄30,性别:男,电话:138000138000,地址:北京
    public static void modify(){
        
        try {
            //创建SAXReader解析器
            SAXReader reader=new SAXReader();
            //创建Document
            Document doc=reader.read("xml/students.xml");
            //找到根节点
            Element root=doc.getRootElement();
            //子节点集合
            List<Element> studentList=root.elements();
            //遍历查找
            for (Element ele : studentList) {
                
                if(ele.attributeValue("id").equals("10013")){
                    ele.element("name").setText("王小五");
                    ele.element("age").setText("30");
                    ele.element("sex").setText("男");
                    ele.element("phone").setText("138000138000");
                    ele.element("address").setText("北京");
                    
                }
            }
                //写入XML
                //设置字符格式和编码
                OutputFormat format=new OutputFormat();
                format.setEncoding("utf-8");
                XMLWriter writer=new XMLWriter(new FileWriter("xml/students.xml"), format);
                //将Document写入
                writer.write(doc);
                //关闭流
                writer.close();
                //给出提示
                System.out.println("====修改成功====");
                
            
            
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

五、删除节点XML

/删除节点 删除王小五的节点
    public static void delete(){
        
        try {
            //创建SAXReader 解析器
            SAXReader reader=new SAXReader();
            //创建Document对象
            Document doc=reader.read("xml/students.xml");
            //获取根节点
            Element root=doc.getRootElement();
            //集合
            List<Element> stuList=root.elements();
            //遍历
            for (Element ele : stuList) {
                if(ele.attributeValue("id").equals("10013")){
                    //获取父节点,并删除这个节点
                    ele.getParent().remove(ele);
                }
            }
            //写入XML
            //设置字符编码格式
            OutputFormat format =new OutputFormat();
            format.setEncoding("utf-8");
            //输出流
            XMLWriter writer=new XMLWriter(new FileWriter("xml/students.xml"), format);
            writer.write(doc);
            writer.close();
            System.out.println("===删除成功===");
            
            
            
            
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

Java从零开始学四十三(DOM4j解析XML)

标签:

原文地址:http://www.cnblogs.com/liunanjava/p/4396599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!