package com.baidu.com;
public class User {
String name = "";
int age = 0;
String adderss = "";
String weight = "";
String sex = "";
String height = "";
public User() {
}
public User(String name, int age, String adderss, String weight, String sex, String height) {
super();
this.name = name;
this.age = age;
this.adderss = adderss;
this.weight = weight;
this.sex = sex;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAdderss() {
return adderss;
}
public void setAdderss(String adderss) {
this.adderss = adderss;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", adderss=" + adderss + ", weight=" + weight + ", sex=" + sex
+ ", height=" + height + "]";
}
}
View Code
package com.baidu.com;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XmlDo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
XmlMapper xmlMapper = new XmlMapper();
//对象转xml
User user = new User("吴建波", 31, "北京市昌平区域沙河在南一村", "118", "男", "168");
String temp = xmlMapper.writeValueAsString(user);
System.out.println(temp);
// xml转对象转json
String temp2 = "<User><name>吴建波</name><age>31</age><adderss>北京市昌平区域沙河在南一村</adderss><weight>118</weight><sex>男</sex><height>168</height></User>";
User userObject = xmlMapper.readValue(temp2, User.class);
System.out.println(userObject);
System.out.println(JSON.toJSONString(userObject));
// xml转Map转Json
String temp3 = "<apps><app><id>1</id><name>Google Maps</name><virsion>1.0</virsion></app></apps>";
temp3 = "<appsii>" + temp3 + "</appsii>";
Map map = xmlMapper.readValue(temp3, HashMap.class);
System.out.println(map);
System.out.println(JSON.toJSONString(map));
}
}
View Code