一、pom

<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>

二、
//用list列表的方式来解析xml
public static void showStr() throws DocumentException {
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n" + "<books>\n" + " <book id=\"005\">\n"
+ " <title>Harry Potter</title>\n" + " <author>J K. Rowling</author>\n" + " </book>\n"
+ " <book id=\"006\">\n" + " <title>Learning XML</title>\n" + " <author>Erik T. Ray</author>\n"
+ " </book>\n" + "</books>";
Document document = DocumentHelper.parseText(str);
Element root = document.getRootElement();
List<Element> childElements = root.elements();
for (Element child : childElements) {
//未知属性名情况下
List<Attribute> attributeList = child.attributes();
for (Attribute attr : attributeList) {
System.out.println(attr.getName() + ": " + attr.getValue());
}
//已知属性名情况下
//System.out.println("id: " + child.attributeValue("id"));

//未知子元素名情况下
List<Element> elementList = child.elements();
for (Element ele : elementList) {
System.out.println(ele.getName() + ": " + ele.getText());
}
System.out.println();
// //已知子元素名的情况下
// System.out.println("title" + child.elementText("title"));
// System.out.println("author" + child.elementText("author"));
// //这行是为了格式化美观而存在
// System.out.println();
}
}

//用list列表的方式来解析xml
public static void show() throws DocumentException {
SAXReader reader = new SAXReader();
File file = new File("D:\\aaaaaaaaaaa\\aa.xml");
Document document = reader.read(file);
Element root = document.getRootElement();
List<Element> childElements = root.elements();
for (Element child : childElements) {
//未知属性名情况下
List<Attribute> attributeList = child.attributes();
for (Attribute attr : attributeList) {
System.out.println(child.getName() + ":" + attr.getName() + ": " + attr.getValue());
}
//已知属性名情况下
//System.out.println("id: " + child.attributeValue("id"));
//未知子元素名情况下
List<Element> elementList = child.elements();
for (Element ele : elementList) {
System.out.println(ele.getName() + ": " + ele.getText());
}
System.out.println();
// //已知子元素名的情况下
// System.out.println("title" + child.elementText("title"));
// System.out.println("author" + child.elementText("author"));
// //这行是为了格式化美观而存在
// System.out.println();
}
}

//创建一个xml
public static void createXML() throws IOException {
Document doc = DocumentHelper.createDocument();
//增加根节点
Element books = doc.addElement("books");
//增加子元素
Element book1 = books.addElement("book");
Element title1 = book1.addElement("title");
Element author1 = book1.addElement("author");
Element book2 = books.addElement("book");
Element title2 = book2.addElement("title");
Element author2 = book2.addElement("author");
//为子节点添加属性
book1.addAttribute("id", "003");
//为元素添加内容
title1.setText("Harry Potter");
author1.setText("J K. Rowling");
book2.addAttribute("id", "004");
title2.setText("Learning XML");
author2.setText("Erik T. Ray");
//实例化输出格式对象
OutputFormat format = OutputFormat.createPrettyPrint();
//设置输出编码
format.setEncoding("UTF-8");
//创建需要写入的File对象
File file = new File("D:" + File.separator + "books.xml");
//生成XMLWriter对象,构造函数中的参数为需要输出的文件流和格式
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format);
//开始写入,write方法中包含上面创建的Document对象
writer.write(doc);
// 立即写入
writer.flush();
// 关闭操作
writer.close();
}


xml事例

<?xml version="1.0" encoding="UTF-8"?>

<books>
  <book id="003">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
  </book>
  <book id="004">
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
  </book>
</books>

 

  xml工具类

/**
*
* @方法声明 读取文件
* @param filePath 文件路径
* @param encoding 读取编码格式
* @return
*/
public static List readXmlFile(String filePath, String encoding) {
try {
if (encoding == null || "".equals(encoding)) {
encoding = "UTF-8";
}
File file = new File(filePath);
List list = new ArrayList<>();
// 判断文件是否存在
if (file.isFile() && file.exists()) {
// 考虑到编码格式
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineText = null;
String allText = "";
int i = 0;
while ((lineText = bufferedReader.readLine()) != null) {
if (i != 0) {
allText = allText + lineText;
}
i++;
}
list = XmlUtil.Xml2Map(allText);
read.close();
return list;
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return null;

}

/**
*
* @方法说明 节点解析
* @param xml
* @return
*/
public static List Xml2Map(String xml) {
Document doc;
try {
doc = DocumentHelper.parseText(xml);
} catch (DocumentException e1) {
e1.printStackTrace();
return null;
}
List list = new ArrayList<>();
if (doc == null)
return list;
Element root = doc.getRootElement();
list = Dom2Map(root);
return list;
}
/**
*
* @方法声明 子节点解析(递归)
* @param root
* @return
*/
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> Dom2Map(Element root) {
List list = new ArrayList<>();
//节点遍历
for (Iterator<Element> iterator = root.elementIterator(); iterator
.hasNext();) {
Element e = (Element) iterator.next();
Map<String, Object> map = new HashMap<String, Object>();
String key = "";
String value = "";
Iterator attr = e.attributes().iterator();
//节点属性遍历
while (attr.hasNext()) {
Attribute attribute = (Attribute) attr.next();
key = attribute.getName();
value = e.attribute(key).getValue();
map.put(key, value);

}
// 判断有无子节点,有就递归解析子节点
if (e.elements().size() > 0) {
map.put("childnode", Dom2Map(e));
list.add(map);
} else {
list.add(map);
}
}
return list;
}