jar准备:dom4j-2.1.1.jar jaxen-1.1.6.jar
jaxen/jaxen/ Maven依赖写法
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>com.springsource.org.dom4j</artifactId>
<version>2.1.1</version>
</dependency>
1 字符串转对象步骤如下:
1 package xml;
2
3 import org.dom4j.Document;
4 import org.dom4j.DocumentHelper;
5 import org.dom4j.Element;
6 import utils.ClassRoom;
7 import utils.Person;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
12 public class ParseXmlStringToBean {
13
14 private static String getPropetyValue(Document doc, String rootPath, String defaultValue) {
15 if (doc.selectNodes(rootPath).size() == 0) {
16 return defaultValue;
17 }
18 return ((Element) doc.selectNodes(rootPath).get(0)).getText();
19 }
20
21 private static int getPropetyValue(Document doc, String rootPath, int defaultValue) {
22 if (doc.selectNodes(rootPath).size() == 0) {
23 return defaultValue;
24 }
25 return Integer.parseInt(((Element) doc.selectNodes(rootPath).get(0)).getText());
26 }
27
28 private static List<Person> parsePerson(Document doc, String rootPath) {
29 if (doc.selectNodes(rootPath).size() == 0) {
30 return null;
31 }
32 int size = doc.selectNodes(rootPath).size();
33 List<Person> personList = new ArrayList<Person>();
34 for (int i = 0; i < size; i++) {
35 Person person = new Person();
36 person.setName(((Element) doc.selectNodes(rootPath + "/" + "name").get(i)).getText());
37 person.setSex(((Element) doc.selectNodes(rootPath + "/" + "sex").get(i)).getText());
38 person.setAge(Integer.parseInt(((Element) doc.selectNodes(rootPath + "/" + "age").get(i)).getText()));
39 personList.add(person);
40 }
41 return personList;
42 }
43
44 private static ClassRoom parseXmlString(String context) throws Exception {
45 ClassRoom classRoom = new ClassRoom();
46 Document doc = DocumentHelper.parseText(context);
47
48 classRoom.setClassName(getPropetyValue(doc, "/data/className", ""));
49 classRoom.setTotalNum(getPropetyValue(doc, "/data/ret_Code", 0));
50 classRoom.setPersonList(parsePerson(doc, "/data/list/items/item"));
51 return classRoom;
52 }
53
54 public static void main(String[] args) throws Exception {
55 String classContent = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
56 "<data><className>1班</className><totailNum>3</totailNum><list><items><item><name>张三</name><sex>男</sex><age>44</age></item><item><name>李四</name><sex>男</sex><age>42</age></item><item><name>王五</name><sex>男</sex><age>22</age></item></items></list></data>\n";
57
58 parseXmlString(classContent);
59 }
60 }
2 对象转字符串
1 package xml;
2
3 import org.dom4j.Document;
4 import org.dom4j.DocumentHelper;
5 import org.dom4j.Element;
6 import utils.ClassRoom;
7 import utils.Person;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
12 public class WriteXmlToString {
13
14 private static String writePersonXml(Person person) {
15 Document doc = DocumentHelper.createDocument();
16 doc.setXMLEncoding("GBK");
17
18 Element root = doc.addElement("data");
19 root.addElement("name").addText(person.getName());
20 root.addElement("sex").addText(person.getSex());
21 root.addElement("age").addText(String.valueOf(person.getAge()));
22 return doc.asXML();
23 }
24
25 private static String writeClassXml(ClassRoom classRoom) {
26 Document doc = DocumentHelper.createDocument();
27 doc.setXMLEncoding("GBK");
28
29 Element root = doc.addElement("data");
30 root.addElement("className").addText(classRoom.getClassName());
31 root.addElement("totailNum").addText(String.valueOf(classRoom.getTotalNum()));
32 Element perList = root.addElement("list");
33 Element perItems = perList.addElement("items");
34 for (Person person : classRoom.getPersonList()) {
35 Element perItem = perList.addElement("item");
36 perItem.addElement("name").addText(person.getName());
37 perItem.addElement("sex").addText(person.getSex());
38 perItem.addElement("age").addText(String.valueOf(person.getAge()));
39 }
40 return doc.asXML();
41 }
42
43 public static void main(String[] args) {
44 String content = writePersonXml(new Person("张三", "男", 44));
45 System.out.println(content);
46
47 List<Person> personList = new ArrayList<Person>();
48 personList.add(new Person("张三", "男", 44));
49 personList.add(new Person("李四", "男", 42));
50 personList.add(new Person("王五", "男", 22));
51 ClassRoom classRoom = new ClassRoom("1班", personList.size(), personList);
52 String classContent = writeClassXml(classRoom);
53 System.out.println(classContent);
54 }
55 }
运行结果
<?xml version="1.0" encoding="GBK"?>
<data>
<name>张三</name>
<sex>男</sex>
<age>44</age>
</data>
<?xml version="1.0" encoding="GBK"?>
<data>
<className>1班</className><totailNum>3</totailNum>
<list>
<items>
<item><name>张三</name><sex>男</sex><age>44</age></item>
<item><name>李四</name><sex>男</sex><age>42</age></item>
<item><name>王五</name><sex>男</sex><age>22</age></item>
</items>
</list>
</data>