Dom4j读写xml文件
程序员文章站
2022-05-29 11:38:18
...
参考文档:
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它地址。如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j。这是必须使用的jar包, Hibernate用它来读写配置文件。
今天项目需要对已存在的xml文件进行更新,对比之下,感觉dom4j还是比较适合的,现在将代码贴上,方便以后参考
/**
*
*/
package com.zlp.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* @author zhangliping
*
*/
public class XmlUtil {
/**
* 将订单信息写入xml文件
* @param map
* @throws DocumentException
* @throws IOException
*/
public void writeXML(Map map) throws DocumentException, IOException {
if (map != null && map.get("respCode").equals("0000")) {// 订单提交成功,未付款
// 将订单信息写入文件
File inputXML = new File("d:/orderList.xml");
// 使用 SAXReader 解析 XML 文档 orderList.xml
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXML);
Element orders = document.getRootElement();// 根节点
Element order = orders.addElement("order");// 订单节点
Element merchantId = order.addElement("merchantId");// 商户ID
merchantId.addAttribute("name", "sitinspring");
merchantId.setText(map.get("merchantId").toString());
Element transType = order.addElement("transType");// 订单状态
transType.setText(map.get("transType") == null ? "00" : map.get(
"transType").toString());
Element merchantOrderId = order.addElement("merchantOrderId");// 订单ID
merchantOrderId.setText(map.get("merchantOrderId").toString());
Element merchantOrderTime = order.addElement("merchantOrderTime");// 订单时间
merchantOrderTime.setText(map.get("merchantOrderTime").toString());
Element merchantOrderAmt = order.addElement("merchantOrderAmt");// 订单金额
merchantOrderAmt.setText(map.get("merchantOrderAmt").toString());
Writer writer = new FileWriter(inputXML);
OutputFormat format = OutputFormat.createPrettyPrint();// 格式化
XMLWriter xmlWriter = new XMLWriter(writer, format);
xmlWriter.write(document);
xmlWriter.close();
}
}
/**
* 读取订单列表xml文件 放入map,多个返回list
*
* @param fileName
* @return
* @throws DocumentException
*/
private List readXML(String fileName) throws DocumentException {
List orderList = new ArrayList();
// 读取文件
File inputXML = new File(fileName);
// 使用SAXReader解析xml
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXML);
Element orders = document.getRootElement();
for (Iterator i = orders.elementIterator(); i.hasNext();) {
Element order = (Element) i.next();
Element merchantId = order.element("merchantId");
Element transType = order.element("transType");
Element merchantOrderId = order.element("merchantOrderId");
Element merchantOrderTime = order.element("merchantOrderTime");
Element merchantOrderAmt = order.element("merchantOrderAmt");
Map map = new HashMap();
map.put("merchantId", merchantId.getText());
map.put("transType", transType.getText());
map.put("merchantOrderId", merchantOrderId.getText());
map.put("merchantOrderTime", merchantOrderTime.getText());
map.put("merchantOrderAmt", merchantOrderAmt.getText());
orderList.add(map);
}
return orderList;
}
/**
* @param args
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException,
IOException {
XmlUtil xmlUtil = new XmlUtil();
HashMap map = new HashMap();
map.put("respCode", "0000");
map.put("merchantId", "merchantId");
map.put("transType", "transType");
map.put("merchantOrderId", "merchantOrderId");
map.put("merchantOrderTime", "merchantOrderTime");
map.put("merchantOrderAmt", "merchantOrderAmt");
xmlUtil.writeXML(map);
}
}
运行效果:
OrderList.xml<?xml version="1.0" encoding="UTF-8"?>
<list>
<order>
<merchantId name="sitinspring">merchantId</merchantId>
<transType>transType</transType>
<merchantOrderId>merchantOrderId</merchantOrderId>
<merchantOrderTime>merchantOrderTime</merchantOrderTime>
<merchantOrderAmt>merchantOrderAmt</merchantOrderAmt>
</order>
</list>
注意:由于Dom4j对于文本的写入带有转义的过程,所以在读取写入的xml时,需要进行转义变换的操作。
转载于:https://my.oschina.net/zlp5201/blog/404235