//使用jaxb對xml進行實體轉換,同時使用trang工具包生成實體類
@Test
public void xml(){
List<User> list = userService.findAll();//查詢的結果
Document document = DocumentHelper.createDocument();
Element nodesElement = document.addElement("allUser");
int i = 0;
for (User o : list) {
Element nodeElement = nodesElement.addElement("oneUser");
if (o instanceof Map) {
for (Object obj : ((Map) o).keySet()) {
Element keyElement = nodeElement.addElement("user");
keyElement.addAttribute("label", String.valueOf(obj));
keyElement.setText(String.valueOf(((Map) o).get(obj)));
}
} else {
Element keyElement = nodeElement.addElement("user");
keyElement.addAttribute("id",o.getId()+"");
keyElement.addAttribute("name",o.getName());
keyElement.addAttribute("password",o.getPassword());
keyElement.addAttribute("gender",o.getGender());
keyElement.addAttribute("age",o.getAge());
keyElement.setText(String.valueOf(o));
}
i++;
}
doc2String(document);
}
//生成xml
public String doc2String(Document document) {
String s = "";
try {
// 使用输出流来进行转化
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 使用UTF-8编码
OutputFormat format = new OutputFormat(" ", true, "UTF-8");
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
s = out.toString("UTF-8");//生成xml
xml2java(s);//转换实体
dom4j(s);//Xpath配合dom4j
dom4jAndXpath(s);
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
//转换实体
public void xml2java(String xml){
JAXBContext jb;
Unmarshaller unmarshaller;
AllUser allUser;
try {
jb = JAXBContext.newInstance(AllUser.class);
unmarshaller = jb.createUnmarshaller();
allUser = (AllUser)unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes()));
String age = allUser.getOneUser().get(0).getUser().getAge();
System.out.println(age);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 這是xml
* <?xml version="1.0" encoding="UTF-8"?>
* <allUser>
* <oneUser>
* <user age="w" gender="w" password="w" name="www" id="40">User [id=40, name=www, password=w, gender=w, age=w]</user>
* </oneUser>
* <oneUser>
* <user age="w" gender="w" password="w" name="www" id="41">User [id=41, name=www, password=w, gender=w, age=w]</user>
* </oneUser>
* </allUser>
* @param xml
*/
//dom4j
public void dom4j(String xml){
Document document;
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Element e = document.getRootElement();//获取根节点(allUser)
List list = e.elements();//获取根节点裡面所有的节点(oneUser)
for(Iterator it = list.iterator();it.hasNext();){
Element el = (Element) it.next();
Element user = el.element("user");//(user)
String age = user.attributeValue("age");
String name = user.attributeValue("name");
String text = user.getTextTrim();
System.out.println(age+"="+name+"="+text);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (UnsupportedEncodingException e1) {
}
}
//Xpath配合dom4j
public void dom4jAndXpath(String xml){
Document document;
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
List list = document.selectNodes("//allUser/oneUser/user");//如果要某一個屬性可以使用@屬性名稱
for(Iterator it = list.iterator();it.hasNext();){
Object obj = it.next();
}
System.out.println(list.size());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (UnsupportedEncodingException e1) {
}
}
//Xstream的使用,也是一種對bean轉換成xml,json
@Test
public void Xstream(){
List<User> list = userService.findAll();//查詢的結果
XStream x = new XStream();
x.alias("User", User.class);//為生成的xml節點重命名
String xml = x.toXML(list);//生成xml
System.out.println(xml);
List<User> user = (List<User>) x.fromXML(xml);//反序列化
System.out.println(user.size());
//序列化成json
XStream xstream = new XStream(new JettisonMappedXmlDriver());
// xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("user", User.class);
String json = xstream.toXML(list);
System.out.println(json);
List<User> jsonUser = (List<User>) xstream.fromXML(json);
System.out.println(jsonUser.size());
@Test
public void xml(){
List<User> list = userService.findAll();//查詢的結果
Document document = DocumentHelper.createDocument();
Element nodesElement = document.addElement("allUser");
int i = 0;
for (User o : list) {
Element nodeElement = nodesElement.addElement("oneUser");
if (o instanceof Map) {
for (Object obj : ((Map) o).keySet()) {
Element keyElement = nodeElement.addElement("user");
keyElement.addAttribute("label", String.valueOf(obj));
keyElement.setText(String.valueOf(((Map) o).get(obj)));
}
} else {
Element keyElement = nodeElement.addElement("user");
keyElement.addAttribute("id",o.getId()+"");
keyElement.addAttribute("name",o.getName());
keyElement.addAttribute("password",o.getPassword());
keyElement.addAttribute("gender",o.getGender());
keyElement.addAttribute("age",o.getAge());
keyElement.setText(String.valueOf(o));
}
i++;
}
doc2String(document);
}
//生成xml
public String doc2String(Document document) {
String s = "";
try {
// 使用输出流来进行转化
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 使用UTF-8编码
OutputFormat format = new OutputFormat(" ", true, "UTF-8");
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
s = out.toString("UTF-8");//生成xml
xml2java(s);//转换实体
dom4j(s);//Xpath配合dom4j
dom4jAndXpath(s);
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
//转换实体
public void xml2java(String xml){
JAXBContext jb;
Unmarshaller unmarshaller;
AllUser allUser;
try {
jb = JAXBContext.newInstance(AllUser.class);
unmarshaller = jb.createUnmarshaller();
allUser = (AllUser)unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes()));
String age = allUser.getOneUser().get(0).getUser().getAge();
System.out.println(age);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 這是xml
* <?xml version="1.0" encoding="UTF-8"?>
* <allUser>
* <oneUser>
* <user age="w" gender="w" password="w" name="www" id="40">User [id=40, name=www, password=w, gender=w, age=w]</user>
* </oneUser>
* <oneUser>
* <user age="w" gender="w" password="w" name="www" id="41">User [id=41, name=www, password=w, gender=w, age=w]</user>
* </oneUser>
* </allUser>
* @param xml
*/
//dom4j
public void dom4j(String xml){
Document document;
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Element e = document.getRootElement();//获取根节点(allUser)
List list = e.elements();//获取根节点裡面所有的节点(oneUser)
for(Iterator it = list.iterator();it.hasNext();){
Element el = (Element) it.next();
Element user = el.element("user");//(user)
String age = user.attributeValue("age");
String name = user.attributeValue("name");
String text = user.getTextTrim();
System.out.println(age+"="+name+"="+text);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (UnsupportedEncodingException e1) {
}
}
//Xpath配合dom4j
public void dom4jAndXpath(String xml){
Document document;
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
List list = document.selectNodes("//allUser/oneUser/user");//如果要某一個屬性可以使用@屬性名稱
for(Iterator it = list.iterator();it.hasNext();){
Object obj = it.next();
}
System.out.println(list.size());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (UnsupportedEncodingException e1) {
}
}
//Xstream的使用,也是一種對bean轉換成xml,json
@Test
public void Xstream(){
List<User> list = userService.findAll();//查詢的結果
XStream x = new XStream();
x.alias("User", User.class);//為生成的xml節點重命名
String xml = x.toXML(list);//生成xml
System.out.println(xml);
List<User> user = (List<User>) x.fromXML(xml);//反序列化
System.out.println(user.size());
//序列化成json
XStream xstream = new XStream(new JettisonMappedXmlDriver());
// xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("user", User.class);
String json = xstream.toXML(list);
System.out.println(json);
List<User> jsonUser = (List<User>) xstream.fromXML(json);
System.out.println(jsonUser.size());
}
总的来说stream要简单些