Java使用DOM4j实现读写XML文件的属性和元素
作者:程沐喆 发布时间:2021-10-19 00:21:52
标签:java,DOM4j,XML
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


猜你喜欢
- 最近在写程序的时候,经常遇到大量需要异步访问的情况,但是对于async和await到底怎么写,还不是非常明确。1.普通的程序怎么写?clas
- 在java中读取读取文件,经常因为路径的问题找不到,此文用于记录如何定位文件的简单方法。本基于springboot做的测试,主要是构建工程方
- 数据层测试事务回滚pom.xml导入对应的一些坐标,mysql,Mp,等<dependency> &
- 我们都知道取消标题栏有两种方式,一种是在Java代码中取消,另一种通过设置styles.xml文件中的Theme即可;如下图:第一种:第二种
- 在为什么阿里巴巴不建议在for循环中使用”+”进行字符串拼接一文中,我们介绍了几种Java中字符串拼接的方式,以及优缺点。其中还有一个重要的
- Android现在这么火,各种的设备也是琳琅满目,高中低等,大小屏幕都有,但是它始终未能达到iOS那样的令人称赞的卓越体验和性能,其操作的流
- 前言传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,这么做有两个缺点:1、如果所有的内容都配置在.xml
- 概述HashTable是jdk 1.0中引入的产物,基本上现在很少使用了,但是会在面试中经常被问到,你都知道吗:HashTable底层的实现
- 我们有很多 Coding Style 或 代码规范。 但这一条可能会经常被我们所遗忘,就是我们 经常会在函数的参数里使用bool参数,这会大
- 废话不多说了,关键代码如下所示:import java.util.*;public class Demo04 {public static
- //创建excelobject missing = System.Reflection.Missing.Value;Excel.Applic
- 代码很简单,功能也很简单,这里就不多废话了#include<stdio.h>int main(){ char ku[16]={&
- 本文实例讲述了C#访问SqlServer设置链接超时的方法。分享给大家供大家参考。具体实现方法如下:下面这段代码设置超时时间为60秒,默认为
- 60年代,在OS中能拥有资源和独立运行的基本单位是进程,然而随着计
- LocalDateTime 是 Java 8 中日期时间 API 提供的一个类,在日期和时间的表示上提供了更加丰富和灵活的支持。LocalD
- 路由做Android/iOS原生开发的时候,要打开一个新的页面,你得知道你的目标页面对象,然后初始化一个Intent或者ViewContro
- 简介通过 pulsar-flink-connector 读取到 Apache pulsar 中的namespaces、topics的元数据信
- TIOBE 11 月编程语言排行榜,Python 逆袭C#曾经有一段时间,脚本语言因其易于编写和易于运行的特性,被预测在未来将发展强大。因此
- 基于有了OO的基础后,开始认真学习设计模式!设计模式是java设计中必不可少的!Apple.javapackage strategy;/**
- 完美地模仿了2048游戏,是根据网友的一个2048改的。Block.javaimport javax.swing.*;import java