JavaWeb_使用dom4j解析、生成XML文件
dom4j 官网
xml解析DOM文档对象模型(树形结构)
DOM方式解析:把xml文档加载到内存形成树形结构,可以进行增删改的操作
Learn
添加jar包进Project中 百度云 传送门 密码:7c8x
1,解析根元素
2,解析有哪些子元素
3,解析一个元素又哪些属性
4,得到元素的文本内容
5,修改、添加、删除某个元素节点
6,修改、添加、删除某个属性
解析的时候可以做的事情
Gary->New->Folder 新建一个lib文件夹
Ctrl C+Ctrl V 将dom4j.jar包复制到lib文件夹中
dom4j.jar->Build Path->Add to Build Path
dom4j解析文件"NewFile.xml"

<?xml version="1.0" encoding="UTF-8"?>
<goodlist>
<good>
<price>12</price>
<name>香蕉</name>
<place>广州</place>
</good>
<good>
<price>39</price>
<name>苹果</name>
<place>北京</place>
</good>
<good>
<price>33</price>
<name>芒果</name>
<place>深圳</place>
</good>
</goodlist>
NewFile.xml
ParseXML解析NewFile.xml中的Dom元素

package Duoxiancheng;
import java.util.Iterator; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class ParseXML {
public static void main(String[] args) throws Exception{
SAXReader reader = new SAXReader();
//获得xml文件路径
Document document = reader.read("src/NewFile.xml");
//获得根节点名
Element root = document.getRootElement();
System.out.println(root.getName());
//获取子元素
Iterator<Element> it = root.elementIterator();
while(it.hasNext()) {
Element ele = it.next();
//获取子元素为name中的文本值
//存在good中没文本会抛出java.lang.NullPointerException空指针异常
if(ele.getName().equals("good")) {
Element name = ele.element("name");
//if(name!=null)
System.out.println(name.getText());
}
//获得子元素名
System.out.println(ele.getName());
Iterator<Attribute> attributes = ele.attributeIterator();
while(attributes.hasNext()) {
Attribute ab = attributes.next();
System.out.println(ab.getName()+":"+ab.getValue());
}
} //xml :Element Attribute
//函数方法输入.后查看
Element ele = null;
//ele.elementIterator(); 遍历方法
//ele. Attribute ab = null;
//ab.
}
}
ParseXML.class
输出:
goodlist
香蕉
good
苹果
good
芒果
good
dom4j生成XML文件

package Duoxiancheng; import java.io.FileWriter;
import java.io.IOException; import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element; public class CreateXML {
public static void main(String[] args) throws IOException {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root"); Element author1 = root.addElement("author")
.addAttribute("name","Gary")
.addAttribute("localtion", "China")
.addText("Hello Gary");
//author1.addElement("添加子标签name") Element author2 = root.addElement("author")
.addAttribute("name","Bob")
.addAttribute("localtion", "US")
.addText("Hello Bob"); //保存文件,运行后刷新一下工程
FileWriter out = new FileWriter("Gary.xml");
document.write(out);
out.close();
}
}
CreateXML.class

JavaWeb_使用dom4j解析、生成XML文件的更多相关文章
- Java中使用DOM4J来生成xml文件和解析xml文件
一.前言 现在有不少需求,是需要我们解析xml文件中的数据,然后导入到数据库中,当然解析xml文件也有好多种方法,小编觉得还是DOM4J用的最多最广泛也最好理解的吧.小编也是最近需求里遇到了,就来整理 ...
- xStream解析生成xml文件学习资料
参考链接: http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
- Android 解析XML文件和生成XML文件
解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...
- 使用XML序列化器生成XML文件和利用pull解析XML文件
首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...
- 使用Pull解析器生成XML文件和读取xml文件
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...
- 利用oxygen编辑并生成xml文件,并使用JAVA的JAXB技术完成xml的解析
首先下载oxygen软件(Oxygen XML Editor),目前使用的是试用版(可以安装好软件以后get trial licence,获得免费使用30天的权限,当然这里鼓励大家用正版软件!!!) ...
- Android 生成xml文件及xml的解析
1.生成xml文件的两种方式 (1)采用拼接的方式生成xml(不推荐使用) (2)利用XmlSerializer类生成xml文件 package com.example.lucky.test52xml ...
- 使用Pull解析器生成XML文件
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...
- dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性
dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性 分类: Java2011-06-03 16:14 976人阅读 评论(0) 收藏 举报 xml扩展语言 今天在开发sitemap地 ...
随机推荐
- tp5.1中redis使用
一.环境安装 1.下载redis,igbniary https://windows.php.net/downloads/pecl/releases/igbinary/ https://windows. ...
- 多进程-Pipe和Manager数据共享和传递
pipe.py#多进程数据传递接收和发送(类似socket) from multiprocessing import Process,Pipe def f(conn): conn.send([42,N ...
- CSP前模板复习
Tarjan 求强连通分量 展开查看 #include #include #include using namespace std; const int N = 1e4 + 1e3; int n, m ...
- neo4j allshortestpaths查询路径不准确问题
同样是5年开发,年薪50万和年薪15万的差距在哪里-.>>> 基本语法 使用neo4j cypher查询语言的小伙伴都知道cypher提供了两个查询最短路径的特殊函数shortest ...
- 给网页中的button加动画效果
网页中的很多事件交互都是通过点击页面中的按钮来实现的,给按钮加一点动画效果也会让网页看起来生动一些,以下就是一个简单的例子: 此按钮的动画主要是通过css的transform动画,伪元素,伪类来实现: ...
- Asp.net4.5未在web服务器上注册
在使用vs2012打开项目时,显示Asp.net4.5未在web服务器上注册?是由于没有下载一个补丁的原因,只需下载安装补丁 VS11-KB3002339.exe 下载地址:https://downl ...
- CentOS7部署HDP3.1.0.0
Apache Ambari是一个基于Web的支持Apache Hadoop集群的供应.管理和监控的开源工具,Ambari已支持大多数Hadoop组件,包括HDFS.MapReduce.Hive.Pig ...
- Delphi 处理异常情况
- centos 7 SVN安装脚本搭建主从同步灵活切换
svn 脚本下载 http://opensource.wandisco.com/subversion_installer_1.9.sh 2019-Aug-20 12:20:4810.1Kapplica ...
- MP4 ISO基础媒体文件格式 摘要 1
目录 Object-structured File Organization 1 File Type Box (ftyp) Box Structures File Structure and gene ...