首页 技术 正文
技术 2022年11月15日
0 收藏 703 点赞 2,941 浏览 3893 个字

一、Dom4j API生成xml文件

  @Test
public void bulidXmlByDom4j(){
//创建document对象
Document document = DocumentHelper.createDocument();
//设置xml文档编码
document.setXMLEncoding("UTF-8");
//创建root根节点
Element root = DocumentHelper.createElement("root");
root.addAttribute("version","1.0");
//创建head节点
Element header = DocumentHelper.createElement("header");
//设置节点的属性
header.addAttribute("version","1.0");
//设置子节点 (子节点无属性时,可直接用addElement创建)
Element element_header1 = header.addElement("timestamp");
//设置子节点的内容
element_header1.setText("20190122");
Element element_header2 = header.addElement("username");
element_header2.setText("yangsj");
Element element_header3 = header.addElement("password");
element_header3.setText("root"); //创建body节点
Element body = DocumentHelper.createElement("body");
body.addAttribute("version","1.0");
//创建action节点
Element action = DocumentHelper.createElement("action");
//创建action的子节点
Element action_option = DocumentHelper.createElement("option");
action_option.addAttribute("name","url");
action_option.addAttribute("value","http://127.0.0.1");
action.add(action_option); //创建data 节点
Element data = DocumentHelper.createElement("data");
Element dataField = DocumentHelper.createElement("field");
dataField.addAttribute("name","money");
dataField.addAttribute("value","10000");
data.add(dataField); body.add(action);
body.add(data); root.add(header);
root.add(body);
document.add(root);
String xml = document.asXML(); System.out.println(xml);
}

  执行结果

<?xml version="1.0" encoding="UTF-8"?>
<root version="1.0">
<header version="1.0">
<timestamp>20190122</timestamp>
<username>yangsj</username>
<password>root</password>
</header>
<body version="1.0">
<action>
<option name="url" value="http://127.0.0.1"/>
</action>
<data>
<field name="money" value="10000"/>
</data>
</body>
</root>

二、Dom4j 解析xml

@Test
public void readXmlInfo() throws DocumentException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root version=\"1.0\"><body " +
"version=\"1.0\"><action><option name=\"url\" value=\"http://127.0.0.1\"/></action><data><field name=\"money\" value=\"10000\"/></data></body><header version=\"1.0\"><timestamp>20190122</timestamp><username>yangsj</username><password>root</password></header></root>"; Map<String,String> headerMap = new HashMap<>();
Map<String,String> actionMap = new HashMap<>();
Map<String,String> dataMap = new HashMap<>(); Document document = DocumentHelper.parseText(xml);
// 获取根元素
Element root = document.getRootElement();
//遍历根元素
for (Iterator iter = root.elementIterator(); iter.hasNext();){
Element element = (Element) iter.next();
//遍历header节点
if("header".equalsIgnoreCase(element.getName())){
for(Iterator i = element.elementIterator(); i.hasNext();){
Element headerElement = (Element) i.next();
headerMap.put(headerElement.getName(),headerElement.getTextTrim());
}
}
//遍历body节点
if("body".equalsIgnoreCase(element.getName())){
for (Iterator j = element.elementIterator(); j.hasNext();){
Element bodyElement = (Element) j.next();
//遍历action节点
if ("action".equalsIgnoreCase(bodyElement.getName())){
for (Iterator k = bodyElement.elementIterator(); k.hasNext();){
Element actionElement = (Element) k.next();
//获取节点的属性值
String name = actionElement.attributeValue("name");
String value = actionElement.attributeValue("value");
actionMap.put(name,value);
}
}
//遍历data节点
if ("data".equalsIgnoreCase(bodyElement.getName())){
for (Iterator k = bodyElement.elementIterator(); k.hasNext();){
Element actionElement = (Element) k.next();
//获取节点的属性值
String name = actionElement.attributeValue("name");
String value = actionElement.attributeValue("value");
dataMap.put(name,value);
}
}
}
} } headerMap.forEach((x,y)->System.out.println("name :" + x + " value :" + y));
System.out.println("------------------------------------");
actionMap.forEach((x,y)->System.out.println("name :" + x + " value :" + y));
System.out.println("------------------------------------");
dataMap.forEach((x,y)->System.out.println("name :" + x + " value :" + y)); }

三、执行结果

name :password value :root
name :timestamp value :20190122
name :username value :yangsj
------------------------------------
name :url value :http://127.0.0.1
------------------------------------
name :money value :10000
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898