xStream转换XML、JSON
一. 简介
xStream可以很容易实现Java对象和xml文档互相转换, 可以修改某个特定的属性和节点名称,xStream提供annotation注解,
可以在JavaBean中完成对xml节点和属性的描述,并支持Json的转换,只需要提供相关的JSONDriver就能完成转换
官方网站: http://xstream.codehaus.org/tutorial.html
二. 准备工作
1. 环境准备:
Jar文件下载地址:
代码结构图:
2. junit测试代码:
- public class XStreamTest {
- private XStream xstream;
- private ObjectOutputStream out;
- private ObjectInputStream in;
- private Student student;
- /**
- * 初始化资源准备
- */
- @Before
- public void init() {
- try {
- xstream = new XStream(new DomDriver());
- } catch (Exception e) {
- e.printStackTrace();
- }
- student = new Student();
- student.setAddress("china");
- student.setEmail("jack@email.com");
- student.setId(1);
- student.setName("jack");
- Birthday birthday = new Birthday();
- birthday.setBirthday("2010-11-22");
- student.setBirthday(birthday);
- }
- /**
- * 释放对象资源
- */
- @After
- public void destory() {
- xstream = null;
- student = null;
- try {
- if (out != null) {
- out.flush();
- out.close();
- }
- if (in != null) {
- in.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.gc();
- }
- /**
- * 打印字符串
- */
- public final void print(String string) {
- System.out.println(string);
- }
- /**
- * 高亮字符串
- */
- public final void highLight(String string) {
- System.err.println(string);
- }
- }
3. 所需实体类:
(1)Student:
- public class Student {
- private int id;
- private String name;
- private String email;
- private String address;
- private Birthday birthday;
- // getter and setter
- public String toString() {
- return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email;
- }
- }
(2)Birthday
- public class Birthday {
- private String birthday;
- public Birthday() {
- }
- public Birthday(String birthday) {
- this.birthday = birthday;
- }
- public String getBirthday() {
- return birthday;
- }
- public void setBirthday(String birthday) {
- this.birthday = birthday;
- }
- }
三 Java对象转为xml
1. 将JavaBean转成xml文档:
- /**
- * Java对象转换成XML
- */
- @Test
- public void writeBean2XML() {
- try {
- highLight("====== Bean -> XML ======");
- print("<!-- 没有重命名的XML -->");
- print(xstream.toXML(student));
- print("<!-- 重命名后的XML -->");
- // 类重命名
- xstream.alias("student", Student.class);
- xstream.alias("生日", Birthday.class);
- xstream.aliasField("生日", Student.class, "birthday");
- xstream.aliasField("生日", Birthday.class, "birthday");
- // 属性重命名
- xstream.aliasField("邮件", Student.class, "email");
- // 包重命名
- xstream.aliasPackage("zdp", "com.zdp.domain");
- print(xstream.toXML(student));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行结果:
- ====== Bean -> XML ======
- <!-- 没有重命名的XML -->
- <com.zdp.domain.Student>
- <id>1</id>
- <name>jack</name>
- <email>jack@email.com</email>
- <address>china</address>
- <birthday>
- <birthday>2010-11-22</birthday>
- </birthday>
- </com.zdp.domain.Student>
- <!-- 重命名后的XML -->
- <student>
- <id>1</id>
- <name>jack</name>
- <邮件>jack@email.com</邮件>
- <address>china</address>
- <生日>
- <生日>2010-11-22</生日>
- </生日>
- </student>
第一份文档是没有经过修改或重命名的文档, 按照原样输出。
第二份文档的类、属性、包都经过了重命名。
2. 将List集合转成xml文档:
- /**
- * 将List集合转换成XML对象
- */
- @Test
- public void writeList2XML() {
- try {
- // 修改元素名称
- highLight("====== List --> XML ======");
- xstream.alias("beans", ListBean.class);
- xstream.alias("student", Student.class);
- ListBean listBean = new ListBean();
- listBean.setName("this is a List Collection");
- List<Object> list = new ArrayList<Object>();
- // 引用javabean
- list.add(student);
- list.add(student);
- // list.add(listBean); 引用listBean,父元素
- student = new Student();
- student.setAddress("china");
- student.setEmail("tom@125.com");
- student.setId(2);
- student.setName("tom");
- Birthday birthday = new Birthday("2010-11-22");
- student.setBirthday(birthday);
- list.add(student);
- listBean.setList(list);
- // 将ListBean中的集合设置空元素,即不显示集合元素标签
- // xstream.addImplicitCollection(ListBean.class, "list");
- // 设置reference模型
- xstream.setMode(XStream.ID_REFERENCES); // id引用
- //xstream.setMode(XStream.NO_REFERENCES); // 不引用
- //xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES); // 绝对路径引用
- // 将name设置为父类(Student)的元素的属性
- xstream.useAttributeFor(Student.class, "name");
- xstream.useAttributeFor(Birthday.class, "birthday");
- // 修改属性的name
- xstream.aliasAttribute("姓名", "name");
- xstream.aliasField("生日", Birthday.class, "birthday");
- print(xstream.toXML(listBean));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行结果:
- ====== List --> XML ======
- <beans id="1">
- <name>this is a List Collection</name>
- <list id="2">
- <student id="3" 姓名="jack">
- <id>1</id>
- <email>jack@email.com</email>
- <address>china</address>
- <birthday id="4" 生日="2010-11-22"/>
- </student>
- <student reference="3"/>
- <student id="5" 姓名="tom">
- <id>2</id>
- <email>tom@125.com</email>
- <address>china</address>
- <birthday id="6" 生日="2010-11-22"/>
- </student>
- </list>
- </beans>
3. 在JavaBean中添加Annotation注解进行重命名设置
(1)JavaBean代码:
- @XStreamAlias("class")
- public class Classes {
- @XStreamAsAttribute
- @XStreamAlias("名称")
- private String name;
- @XStreamOmitField
- private int number;
- @XStreamImplicit(itemFieldName = "Students")
- private List<Student> students;
- @XStreamConverter(SingleValueCalendarConverter.class)
- private Calendar created = new GregorianCalendar();
- public Classes() {
- }
- public Classes(String name, Student... stu) {
- this.name = name;
- this.students = Arrays.asList(stu);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- public List<Student> getStudents() {
- return students;
- }
- public void setStudents(List<Student> students) {
- this.students = students;
- }
- public Calendar getCreated() {
- return created;
- }
- public void setCreated(Calendar created) {
- this.created = created;
- }
- }
(2)编写类型转换器:
- public class SingleValueCalendarConverter implements Converter {
- public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
- Calendar calendar = (Calendar) source;
- writer.setValue(String.valueOf(calendar.getTime().getTime()));
- }
- public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
- GregorianCalendar calendar = new GregorianCalendar();
- calendar.setTime(new Date(Long.parseLong(reader.getValue())));
- return calendar;
- }
- public boolean canConvert(Class type) {
- return type.equals(GregorianCalendar.class);
- }
- }
(3)测试代码:
- /**
- * 使用注解将List转为XML文档
- */
- @Test
- public void writeList2XML4Annotation() {
- try {
- highLight("====== annotation Bean --> XML ======");
- Student stu = new Student();
- stu.setName("jack");
- Classes c = new Classes("一班", student, stu);
- c.setNumber(2);
- xstream.alias("student", Student.class);
- print(xstream.toXML(c));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行结果:
- ====== annotation Bean --> XML ======
- <com.zdp.domain.Classes>
- <name>一班</name>
- <number>2</number>
- <students class="java.util.Arrays$ArrayList">
- <a class="student-array">
- <student>
- <id>1</id>
- <name>jack</name>
- <email>jack@email.com</email>
- <address>china</address>
- <birthday>
- <birthday>2010-11-22</birthday>
- </birthday>
- </student>
- <student>
- <id>0</id>
- <name>jack</name>
- </student>
- </a>
- </students>
- <created>
- <time>1409821431920</time>
- <timezone>Asia/Shanghai</timezone>
- </created>
- </com.zdp.domain.Classes>
4. 将Map集合转成xml文档:
- /**
- * 将Map集合转成XML文档
- */
- @Test
- public void writeMap2XML() {
- try {
- highLight("====== Map --> XML ======");
- Map<String, Student> map = new HashMap<String, Student>();
- map.put("No.1", student);
- student = new Student();
- student.setAddress("china");
- student.setEmail("tom@125.com");
- student.setId(2);
- student.setName("tom");
- Birthday day = new Birthday("2010-11-22");
- student.setBirthday(day);
- map.put("No.2", student);
- student = new Student();
- student.setName("jack");
- map.put("No.3", student);
- xstream.alias("student", Student.class);
- xstream.alias("key", String.class);
- xstream.useAttributeFor(Student.class, "id");
- xstream.useAttributeFor("birthday", String.class);
- print(xstream.toXML(map));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行结果:
- ====== Map --> XML ======
- <map>
- <entry>
- <key>No.3</key>
- <student id="0">
- <name>jack</name>
- </student>
- </entry>
- <entry>
- <key>No.1</key>
- <student id="1">
- <name>jack</name>
- <email>jack@email.com</email>
- <address>china</address>
- <birthday birthday="2010-11-22"/>
- </student>
- </entry>
- <entry>
- <key>No.2</key>
- <student id="2">
- <name>tom</name>
- <email>tom@125.com</email>
- <address>china</address>
- <birthday birthday="2010-11-22"/>
- </student>
- </entry>
- </map>
5. 用OutStream输出流写XML
- /**
- * 用OutStream输出流写XML
- */
- @Test
- public void writeXML4OutStream() {
- try {
- out = xstream.createObjectOutputStream(System.out);
- Student stu = new Student();
- stu.setName("jack");
- Classes c = new Classes("一班", student, stu);
- c.setNumber(2);
- highLight("====== ObjectOutputStream ## JavaObject--> XML ======");
- out.writeObject(stu);
- out.writeObject(new Birthday("2010-05-33"));
- out.write(22);//byte
- out.writeBoolean(true);
- out.writeFloat(22.f);
- out.writeUTF("hello");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行结果:
- ====== ObjectOutputStream ## JavaObject--> XML ======
- <object-stream>
- <com.zdp.domain.Student>
- <id>0</id>
- <name>jack</name>
- </com.zdp.domain.Student>
- <com.zdp.domain.Birthday>
- <birthday>2010-05-33</birthday>
- </com.zdp.domain.Birthday>
- <byte>22</byte>
- <boolean>true</boolean>
- <float>22.0</float>
- <string>hello</string>
- </object-stream>
四. xml文档转为Java对象:
1. 用inputStream将XML文档转换为Java对象
- /**
- * 用InputStream将XML文档转换成java对象
- */
- @Test
- public void readXML4InputStream() {
- try {
- String s = "<object-stream><com.zdp.domain.Student><id>0</id><name>jack</name>" +
- "</com.zdp.domain.Student><com.zdp.domain.Birthday><birthday>2010-05-33</birthday>" +
- "</com.zdp.domain.Birthday><byte>22</byte><boolean>true</boolean><float>22.0</float>" +
- "<string>hello</string></object-stream>";
- highLight("====== ObjectInputStream## XML --> javaObject ======");
- StringReader reader = new StringReader(s);
- in = xstream.createObjectInputStream(reader);
- Student stu = (Student) in.readObject();
- Birthday b = (Birthday) in.readObject();
- byte i = in.readByte();
- boolean bo = in.readBoolean();
- float f = in.readFloat();
- String str = in.readUTF();
- System.out.println(stu);
- System.out.println(b);
- System.out.println(i);
- System.out.println(bo);
- System.out.println(f);
- System.out.println(str);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行结果:
- ====== ObjectInputStream## XML --> javaObject ======
- jack#0#null#null#null
- com.zdp.domain.Birthday@27391d
- 22
- true
- 22.0
- hello
2. 将XML文档转为Java对象:
- /**
- * 将XML文档转换成Java对象
- */
- @Test
- public void readXml2Object() {
- try {
- highLight("====== Xml >>> Bean ======");
- Student stu = (Student) xstream.fromXML(xstream.toXML(student));
- print(stu.toString());
- List<Student> list = new ArrayList<Student>();
- list.add(student);//add
- Map<String, Student> map = new HashMap<String, Student>();
- map.put("No.1", student);//put
- student = new Student();
- student.setAddress("china");
- student.setEmail("tom@125.com");
- student.setId(2);
- student.setName("tom");
- Birthday day = new Birthday("2010-11-22");
- student.setBirthday(day);
- list.add(student);//add
- map.put("No.2", student);//put
- student = new Student();
- student.setName("jack");
- list.add(student);//add
- map.put("No.3", student);//put
- highLight("====== XML >>> List ======");
- List<Student> studetns = (List<Student>) xstream.fromXML(xstream.toXML(list));
- print("size:" + studetns.size());//3
- for (Student s : studetns) {
- print(s.toString());
- }
- highLight("====== XML >>> Map ======");
- Map<String, Student> maps = (Map<String, Student>) xstream.fromXML(xstream.toXML(map));
- print("size:" + maps.size());//3
- Set<String> key = maps.keySet();
- Iterator<String> iter = key.iterator();
- while (iter.hasNext()) {
- String k = iter.next();
- print(k + ":" + map.get(k));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行结果:
- ====== Xml >>> Bean ======
- jack#1#china#com.zdp.domain.Birthday@1125127#jack@email.com
- ====== XML >>> List ======
- size:3
- jack#1#china#com.zdp.domain.Birthday@134bed0#jack@email.com
- tom#2#china#com.zdp.domain.Birthday@1db4f6f#tom@125.com
- jack#0#null#null#null
- ====== XML >>> Map ======
- size:3
- No.3:jack#0#null#null#null
- No.1:jack#1#china#com.zdp.domain.Birthday@1d520c4#jack@email.com
- No.2:tom#2#china#com.zdp.domain.Birthday@2a5330#tom@125.com
五. xStream对JSON的支持:
xStream对JSON也有非常好的支持,它提供了2个模型驱动。用这2个驱动可以完成Java对象到JSON的相互转换。使用JettisonMappedXmlDriver驱动,将Java对象转换成json,需要添加jettison.jar
1. 用JettisonMappedXmlDriver完成Java对象到JSON的转换
- /**
- * XStream结合JettisonMappedXmlDriver驱动,转换Java对象到JSON
- */
- @Test
- public void writeEntity2JETTSON() {
- highLight("====== JettisonMappedXmlDriver === JavaObject >>>> JaonString ======");
- xstream = new XStream(new JettisonMappedXmlDriver());
- xstream.setMode(XStream.NO_REFERENCES);
- xstream.alias("student", Student.class);
- print(xstream.toXML(student));
- }
运行结果:
- ====== JettisonMappedXmlDriver === JavaObject >>>> JaonString ======
- {"student":{"id":1,"name":"jack","email":"jack@email.com","address":"china","birthday":[{},"2010-11-22"]}}
2. 用JsonHierarchicalStreamDriver完成Java对象到JSON的转换
- /**
- * 转换java对象为JSON字符串
- */
- @Test
- public void writeEntiry2JSON() {
- highLight("====== JsonHierarchicalStreamDriver === JavaObject >>>> JaonString ======");
- xstream = new XStream(new JsonHierarchicalStreamDriver());
- xstream.alias("student", Student.class);
- highLight("-------Object >>>> JSON---------");
- print(xstream.toXML(student));
- //删除根节点
- xstream = new XStream(new JsonHierarchicalStreamDriver() {
- public HierarchicalStreamWriter createWriter(Writer out) {
- return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
- }
- });
- xstream.alias("student", Student.class);
- print(xstream.toXML(student));
- }
运行结果:
- ====== JsonHierarchicalStreamDriver === JavaObject >>>> JaonString ======
- -------Object >>>> JSON---------
- {"student": {
- "id": 1,
- "name": "jack",
- "email": "jack@email.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- }}
- {
- "id": 1,
- "name": "jack",
- "email": "jack@email.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- }
使用JsonHierarchicalStreamDriver转换默认会给转换后的对象添加一个根节点,但是在构建JsonHierarchicalStreamDriver驱动的时候,
你可以重写createWriter方法,删掉根节点。
3. 将List集合转换成JSON串
- /**
- * 将List集合转换成JSON字符串
- */
- @Test
- public void writeList2JSON() {
- highLight("===== JsonHierarchicalStreamDriver ==== JavaObject >>>> JaonString =====");
- JsonHierarchicalStreamDriver driver = new JsonHierarchicalStreamDriver();
- xstream = new XStream(driver);
- // xstream = new XStream(new JettisonMappedXmlDriver());//转换错误
- // xstream.setMode(XStream.NO_REFERENCES);
- xstream.alias("student", Student.class);
- List<Student> list = new ArrayList<Student>();
- list.add(student);
- student = new Student();
- student.setAddress("china");
- student.setEmail("tom@125.com");
- student.setId(2);
- student.setName("tom");
- Birthday day = new Birthday("2010-11-22");
- student.setBirthday(day);
- list.add(student);
- student = new Student();
- student.setName("jack");
- list.add(student);
- print(xstream.toXML(list));
- //删除根节点
- xstream = new XStream(new JsonHierarchicalStreamDriver() {
- public HierarchicalStreamWriter createWriter(Writer out) {
- return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
- }
- });
- xstream.alias("student", Student.class);
- print(xstream.toXML(list));
- }
运行结果:
- ===== JsonHierarchicalStreamDriver ==== JavaObject >>>> JaonString =====
- {"list": [
- {
- "id": 1,
- "name": "jack",
- "email": "jack@email.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- },
- {
- "id": 2,
- "name": "tom",
- "email": "tom@125.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- },
- {
- "id": 0,
- "name": "jack"
- }
- ]}
- [
- {
- "id": 1,
- "name": "jack",
- "email": "jack@email.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- },
- {
- "id": 2,
- "name": "tom",
- "email": "tom@125.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- },
- {
- "id": 0,
- "name": "jack"
- }
- ]
4. 将Map转换成json串:
- /**
- * 将Map集合转换成JSON字符串
- */
- @Test
- public void writeMap2JSON() {
- highLight("==== JsonHierarchicalStreamDriver ==== Map >>>> JaonString =====");
- xstream = new XStream(new JsonHierarchicalStreamDriver());
- xstream.alias("student", Student.class);
- Map<String, Student> map = new HashMap<String, Student>();
- map.put("No.1", student);
- student = new Student();
- student.setAddress("china");
- student.setEmail("tom@125.com");
- student.setId(2);
- student.setName("tom");
- student.setBirthday(new Birthday("2010-11-21"));
- map.put("No.2", student);
- student = new Student();
- student.setName("jack");
- map.put("No.3", student);
- print(xstream.toXML(map));
- //删除根节点
- xstream = new XStream(new JsonHierarchicalStreamDriver() {
- public HierarchicalStreamWriter createWriter(Writer out) {
- return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
- }
- });
- xstream
运行结果:
- ==== JsonHierarchicalStreamDriver ==== Map >>>> JaonString =====
- {"map": [
- [
- "No.3",
- {
- "id": 0,
- "name": "jack"
- }
- ],
- [
- "No.1",
- {
- "id": 1,
- "name": "jack",
- "email": "jack@email.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- }
- ],
- [
- "No.2",
- {
- "id": 2,
- "name": "tom",
- "email": "tom@125.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-21"
- }
- }
- ]
- ]}
- [
- [
- "No.3",
- {
- "id": 0,
- "name": "jack"
- }
- ],
- [
- "No.1",
- {
- "id": 1,
- "name": "jack",
- "email": "jack@email.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-22"
- }
- }
- ],
- [
- "No.2",
- {
- "id": 2,
- "name": "tom",
- "email": "tom@125.com",
- "address": "china",
- "birthday": {
- "birthday": "2010-11-21"
- }
- }
- ]
- ]
5. 将JSON转换成Java对象:
- /**
- * 将JSON字符串转换成java对象
- */
- @Test
- public void readJSON2Object() throws JSONException {
- String json = "{student: {" +
- "id: 1," +
- "name: haha," +
- "email: email," +
- "address: address," +
- "birthday: {" +
- "birthday: 2010-11-22 " +
- "}" +
- "}}";
- xstream = new XStream(new JettisonMappedXmlDriver());
- xstream.alias("student", Student.class);
- print(xstream.fromXML(json).toString());
- json = "{list: [{" +
- "id: 1," +
- "name: haha," +
- "email: email," +
- "address: address," +
- "birthday: {" +
- "birthday: 2010-11-22" +
- "}" +
- "},{" +
- "id: 2," +
- "name: tom," +
- "email: tom@125.com," +
- "address: china," +
- "birthday: {" +
- "birthday: 2010-11-22" +
- "}" +
- "}" +
- "]}";
- System.out.println(json);
- List list = (List) xstream.fromXML(json);
- System.out.println(list.size());
- }
运行结果:
- haha#1#address#com.zdp.domain.Birthday@137c60d#email
- {list: [{id: 1,name: haha,email: email,address: address,birthday: {birthday: 2010-11-22}},{id: 2,name: tom,email: tom@125.com,address: china,birthday: {birthday: 2010-11-22}}]}
- 0
三. 遇到的问题
1. 如何加上xml头部?即<?xml version="1.0" encoding="UTF-8"?>
官方文档是这样解释的:
Why does XStream not write an XML declaration?
XStream is designed to write XML snippets, so you can embed its output into an existing stream or string.
You can write the XML declaration yourself into the Writer before using it to call XStream.toXML(writer).
我们可以自己添加:XmlDeclarationXStream
- public class XmlDeclarationXStream extends XStream {
- private String version;
- private String ecoding;
- public XmlDeclarationXStream() {
- this("1.0", "utf-8");
- }
- public XmlDeclarationXStream(String version, String ecoding) {
- this.version = version;
- this.ecoding = ecoding;
- }
- public String getDeclaration() {
- return "<?xml version=\"" + this.version + "\" encoding=\"" + this.ecoding + "\"?>";
- }
- @Override
- public void toXML(Object obj, OutputStream output) {
- try {
- String dec = this.getDeclaration();
- byte[] bytesOfDec = dec.getBytes(this.ecoding);
- output.write(bytesOfDec);
- } catch (Exception e) {
- throw new RuntimeException("error happens", e);
- }
- super.toXML(obj, output);
- }
- @Override
- public void toXML(Object obj, Writer writer) {
- try {
- writer.write(getDeclaration());
- } catch (Exception e) {
- throw new RuntimeException("error happens", e);
- }
- super.toXML(obj, writer);
- }
- }
测试的时候我们new这个类:XStream xstream = new XmlDeclarationXStream();
源码下载:http://download.csdn.net/detail/zdp072/7866129
原文:http://blog.csdn.net/IBM_hoojo/article/details/6342386
xStream转换XML、JSON的更多相关文章
- xStream完美转换XML、JSON
xStream框架 xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换: 前面有介绍过json-lib这个框架,在线博文:htt ...
- xStream完美转换XML、JSON(转)
xStream框架 xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换: 前面有介绍过json-lib这个框架,在线博文:htt ...
- Java对象转换成xml对象和Java对象转换成JSON对象
1.把Java对象转换成JSON对象 apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串.也可以把JavaBean转换成JSON字符串. json-lib的核心 ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析](使用JAXB转换XML)
前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ...
- 如何在ASP.NET中用C#将XML转换成JSON
本文旨在介绍如果通过C#将获取到的XML文档转换成对应的JSON格式字符串,然后将其输出到页面前端,以供JavaScript代码解析使用.或许你可以直接利用JavaScript代码通过Ajax的方式来 ...
- 如何在ASP.NET中用C#将XML转换成JSON 【转】
本文旨在介绍如果通过C#将获取到的XML文档转换成对应的JSON格式字符串,然后将其输出到页面前端,以供JavaScript代码解析使用.或许你可以直接利用JavaScript代码通过Ajax的方 ...
- C#将XML转换成JSON转换XML
原文:C#将XML转换成JSON转换XML using System; using System.Collections.Generic; using System.Linq; using Syste ...
- REST Adapter实现SAP PI中的增强XML/JSON格式转换
SAP标准的REST adapter有着XML/JSON转换的功能,它很有用,因为一方面SAP PI/PO内部以XML格式处理数据,而另一方面,在处理REST架构风格的时候,JSON才是事实上的格式. ...
- json串转化成xml文件、xml文件转换成json串
1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...
随机推荐
- go http client, http server
Go语言中的HTTP client, server非常简单.具体如下. HTTP Server package main import ( "fmt" "html&quo ...
- box-shadow 边框样式
如下 box-shadow: 0 1px 3px 0 rgba(0,0,0,.2), 0 1px 1px 0 rgba(0,0,0,.14), 0 2px 1px -1px rgba(0,0,0,.1 ...
- Eclipse设置相同变量背景色高亮显示
在Eclipse中,鼠标选中或者光标移动到java类的变量名时,相同变量会被标识显示(设置背景色高亮), 并且侧边滚动条会标出变量的位置, 查找变量十分方便. 1.相同变量标识高亮显示: Window ...
- 知识点查缺补漏贴02:Linux环境fork()函数详解
引言 先来看一段代码吧, #include <sys/types.h> #include <unistd.h> #include <stdio.h> #includ ...
- ubuntu 14.04 git clone 出现 fatal: Unable to find remote helper for 'https'
当你编译安装git时因为没有安装(lib)curl-devel所以导致git clone 和 git push 都会出现这个错误 如果你安装了(lib)curl-devel,然后重新编译安装git就没 ...
- Date类为什么设计为可变的,而不是像String一样?
首先,不得不承认,这确实是类库设计的一个错误,所以"为什么"进行了这个错误设计并没有意义.但没有事物一诞生就是完美的,我们的Java只是反应的慢了一点,再慢了一点. 更何况,Dat ...
- 关于String.valueOf()和.toString的问题
以下是String.valueOf()的源代码 public static String valueOf(Object obj) { return (obj == null) ? " ...
- position和margin的绝对定位和相对定位
电脑桌面清理干净之后,果然快了很多,桌面上的东西会占用内存,导致电脑变慢,以前我看到表姐的电脑桌面堆满了东西,我就在心里默默的鄙视不懂玩电脑的人,现在我竟然也养成了这种坏毛病..保存东西的时候放在桌面 ...
- django中视图处理请求方式(FBV、CBV)
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...
- gz文件最后四位检测
[root@node-0 ~]# ll -rw-r--r-- 1 root root 24048 Nov 29 11:29 install.log 文件大小为24048 [root@node-0 ~ ...