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 = ; i < ; 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", "");
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 = ; i < ; 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("");
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("");
Element ContactName = element.addElement("");
ContactName.setText("" );
} }
XMLWriter writer = new XMLWriter(new FileOutputStream(file));
writer.write(doc);
ReadAttributes();
} catch (Exception e) {
// TODO: handle exception
} }

Java:使用DOM4j来实现读写XML文件中的属性和元素的更多相关文章

  1. C#程序中:如何删除xml文件中的节点、元素。

    C#中动态的清理xml文件中的垃圾信息是程序员必会的哦.这就像数据库一样,不会清理数据怎么可以呢?其实xml文件就可以用作一个小的数据库,存储一些简单的信息.所以,用C#程序实现xml文件的增.删.改 ...

  2. vue项目中使用bpmn-流程图xml文件中节点属性转json结构

    内容概述 本系列“vue项目中使用bpmn-xxxx”分为七篇,均为自己使用过程中用到的实例,手工原创,目前陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项,具 ...

  3. Java 使用Dom4j和JFileChooser实现xml文件的自主选择路径导出

    直接来个简单的例子,大家一看便知. Document doc=DocumentHelper.createDocument();//创建document Element rootElement=doc. ...

  4. Java从入门到精通——技巧篇之利用dom4j取出XML文件中的数据

    在我们做项目的时候会经常用到XML文件用来配置系统,XML让系统更加的具有了灵活性,Java如何从XML中取出我们想要的数据呢?下面是我利用DOM4J来实现取出XML文件中的数据. XML文件 < ...

  5. web.xml文件中的7个错误的安全配置

    web.xml文件中的7个错误的安全配置 关于Java的web.xml文件中配置认证和授权有大 量 的 文章.本文不再去重新讲解如何配置角色.保护web资源和设置不同类型的认证,让我们来看看web.x ...

  6. C#程序中:如何修改xml文件中的节点(数据)

    要想在web等程序中实现动态的数据内容给新(如网页中的Flash),不会更新xml文件中的节点(数据)是远远不够的,今天在这里说一个简单的xml文件的更新,方法比较基础,很适合初学者看的,保证一看就懂 ...

  7. DirectUI界面编程(三)从XML文件中加载界面

    Duilib支持xml界面布局,使得界面设计与逻辑处理相分离,本节介绍如何从xml文件中加载界面元素. 我们需要以下几个步骤: 创建并初始化CPaintManagerUI对象. 创建CDialogBu ...

  8. Java 读写XML文件 API--org.dom4j

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...

  9. java通过dom读写xml文件

    java通过dom读写xml文件 要读的xml文件 <?xml version="1.0" encoding="GB2312"?><学生花名册 ...

随机推荐

  1. S11 day 94 RestFramework 之 APIView视图

    VIEW视图(Django自带的) 1.  url url(r'login/$', views.login.as_view()), 2.点开 as_view() , as_view()为类方法.  l ...

  2. Weekly Contest 132

    1025. Divisor Game Alice and Bob take turns playing a game, with Alice starting first. Initially, th ...

  3. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  4. jQuery.extend 函数

    jQuery.extend 函数详解 JQuery的extend扩展方法: Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解. 一.J ...

  5. linux centos7 防火墙及端口开放相关命令

    一.防火墙相关命令 1.查看防火墙状态 : systemctl status firewalld.service 注:active是绿的running表示防火墙开启 2.关闭防火墙 :systemct ...

  6. AngularJS Directive 命名规则

    使用规则 在HTML代码中,使用的是连接符的形式,下面我们对比看看命名和使用的对应字符串: 命名 使用 people people peopleList people-list peopleListA ...

  7. Collection、Set、List概念上的区别及关联

    类图如下:

  8. Redis查询&JDBC查询&Hibernate查询方式的效率比较...

    比较三种查询方式查询效率对比...我是用的JavaWeb的方式通过通过JSP页面查询的填写查询的参数...给予反馈.... 整个demo的下载地址:http://files.cnblogs.com/f ...

  9. python代码位置引发的错误

    觉得python对代码位置的要求简直是变态,缩进引发的错误我以前在博客里讲过了,如果不懂可以看看我以前的博客,今天又遇到了一个代码位置所引发的错误,现在给大家分享一下: 我想要打印出来一个5*5的实心 ...

  10. C/C++ -- Gui编程 -- Qt库的使用 -- 理解主窗体构造函数

    MyWidget做父窗体 MyWidget的构造函数中可以手动添加组件 Ui::MyWidget存放子部件 Ui::MyWidget执行setupUi()函数为子部件开辟空间,指定父窗体 MyWidg ...