博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java:使用DOM4j来实现读写XML文件中的属性和元素
阅读量:5307 次
发布时间:2019-06-14

本文共 6946 字,大约阅读时间需要 23 分钟。

DOM4可以读取和添加XML文件的属性或者元素

读取属性:

public static void ReadAttributes() throws DocumentException {        File file = new File("D:\\cmz\\java\\XMLTest\\Customertest.xml");        SAXReader reader = new SAXReader();        Document doc = reader.read(file);        Element root = doc.getRootElement();        try {            for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {                Element element = (Element) iterator.next();                String CustomerID = element.attributeValue("CustomerID");                System.out.println("CustomerID = " + CustomerID);                String CompanyName = element.attributeValue("CompanyName");                System.out.println("CompanyName =  " + CompanyName);                System.out.println("ContactName = "                        + element.attributeValue("ContactName"));                System.out.println("ContactTitle = "                        + element.attributeValue("ContactTitle"));                System.out.println("Address = "                        + element.attributeValue("Address"));                System.out.println("City = " + element.attributeValue("Cit阿y"));                System.out.println("PostalCode = "                        + element.attributeValue("PostalCode"));                System.out.println("Country = "                        + element.attributeValue("Country"));                System.out                        .println("Phone = " + element.attributeValue("Phone"));                System.out.println("Fax = " + element.attributeValue("Fax"));                System.out                        .println("--------------------------------------------------------\t");            }        } catch (Exception e) {            // TODO: handle exception        }    }

读取元素:

public static void ReadInnerTest() throws DocumentException {        File file = new File("D:\\cmz\\java\\XMLTest\\Customer1.xml");        SAXReader reader = new SAXReader();        Document doc = reader.read(file);        Element root = doc.getRootElement();        try {            for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {                Element type = (Element) iterator.next();                                System.out.println(type.elementText("CustomerID"));                System.out.println(type.elementText("CompanyName"));                System.out.println(type.elementText("ContactName"));                System.out.println(type.elementText("ContactTitle"));                System.out.println(type.elementText("Address"));                System.out.println(type.elementText("City"));                System.out.println(type.elementText("PostalCode"));                System.out.println(type.elementText("Country"));                System.out.println(type.elementText("Phone"));                System.out.println(type.elementText("Fax"));                System.out.println("---------------------------------\t");            }        } catch (Exception e) {            // TODO: handle exception        }    }

 

写入属性:

public static void WriteAttributes() {        Document doc = DocumentHelper.createDocument();        Element ele = doc.addElement("table");        for (int i = 1; i < 5; i++) {            Element Customers = ele.addElement("Customers");                        Customers.addAttribute("CustomerID", "ALFKI" + i);            Customers.addAttribute("CompanyName", "Alfreds Futterkiste" + i);            Customers.addAttribute("ContactName", "Maria Anders" + i);            Customers.addAttribute("ContactTitle", "Sales Representative" + i);            Customers.addAttribute("Address", "Obere Str. 57");            Customers.addAttribute("City", "beijin");            Customers.addAttribute("PostalCode", "12209");            Customers.addAttribute("Country", "Germany");            Customers.addAttribute("Phone", "030-0074321");            Customers.addAttribute("Fax", "030-0076545");            try {                XMLWriter writer = new XMLWriter(new FileWriter(new File(                        "Customertest.xml")));                writer.write(doc);                writer.close();            } catch (Exception e) {                // TODO: handle exception            }        }    }

 

写出元素:

public static void  writeInnerTest(){        Document doc = DocumentHelper.createDocument();        Element ele = doc.addElement("table");                for (int i = 1; i < 5; i++) {            Element Customers = ele.addElement("row");            Element CustomerID = ele.addElement("CustomerID");            CustomerID.setText("ALFKI" + i);            Element CompanyName = ele.addElement("CompanyName");            CompanyName.setText("Alfreds Futterkiste" + i);            Element ContactName = ele.addElement("ContactName");            ContactName.setText("Maria Anders" + i);            Element ContactTitle = ele.addElement("ContactTitle");            ContactTitle.setText("Sales Representative" + i);            Element Address = ele.addElement("Address");            Address.setText("Obere Str. 57");            Element City = ele.addElement("City");            City.setText("beijin");            Element PostalCode = ele.addElement("PostalCode");            PostalCode.setText("12209");            Element Country = ele.addElement("Country");            Country.setText("Germany");            Element Phone = ele.addElement("Phone");            Phone.setText("030-0074321");            Element Fax = ele.addElement("Fax");            Fax.setText("030-0076545");        }        try {            XMLWriter writer = new XMLWriter(new FileWriter(new File(                    "Customertest2.xml")));            writer.write(doc);            writer.close();        } catch (Exception e) {            // TODO: handle exception        }                    }

 

可以修改属性的文本内容:

public static void ReadUpdateattribute() throws DocumentException{        File file = new File("D:\\cmz\\java\\XMLTest\\Customertest.xml");        SAXReader reader = new SAXReader();        Document doc = reader.read(file);        Element root = doc.getRootElement();                try {            for (Iterator iterator = root.elementIterator("Customers"); iterator.hasNext();) {                Element element = (Element) iterator.next();                String name = "ALFKI1";                if (name.equals(element.attributeValue("CustomerID")) ) {                    Attribute attr = element.attribute("CustomerID");                    attr.setValue("234");                    Element ContactName = element.addElement("23424");                    ContactName.setText("676767" );                }                                                                }            XMLWriter writer = new XMLWriter(new FileOutputStream(file));            writer.write(doc);            ReadAttributes();        } catch (Exception e) {            // TODO: handle exception        }            }

 

转载于:https://www.cnblogs.com/chengmuzhe/p/10127895.html

你可能感兴趣的文章
JMeter上传文件
查看>>
MVC+三层架构
查看>>
【06】GitHub WiKi
查看>>
【05】 定义类或对象
查看>>
package control(转载)
查看>>
POJ3281 Dining 最大流
查看>>
php 协程理解
查看>>
Linux ntpdate命令详解
查看>>
关于tuple的只读特性
查看>>
Unicode字符集和多字节字符集关系(转载)
查看>>
昨天晚上加班到9:15
查看>>
虚拟机linux联网
查看>>
设计模式-代理模式
查看>>
面试2018
查看>>
BZOJ 1026 windy数
查看>>
Vue 基础精讲
查看>>
verilog 数组参数
查看>>
JS Object对象的方法总结( ES5 与 ES6 )
查看>>
Airflow 调度基础
查看>>
Centos 6安装 Jenkins
查看>>