xstream xml转化java时,节点大小写转化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xstream xml转化java时,节点大小写转化相关的知识,希望对你有一定的参考价值。

XStream xs = new XStream(new DomDriver());
Employee e = new Employee();
xs.alias("Employee", Employee.class);
xs.aliasField("message", Message.class, "Message");
e.setName("tom");
e.setAge("18");
e.setDepartment("asd");
e.setDesignation("heh");
Message message = new Message();
message.setPhone("123");
message.setAddress("nanjing");
e.setMessage(message);
String str = e.toString();
System.out.println(xs.toXML(e));

输出结果:

<Employee>
<name>tom</name>
<designation>heh</designation>
<department>asd</department>
<age>18</age>
<message>
<phone>123</phone>
<address>nanjing</address>
</message>
</Employee>

在输出Message时,我已经改过别名了,为什么输出还是小写的

参考技术A 晕,
xs.aliasField("message",Employee.class, "Message");
是这个样子的...本回答被提问者采纳

xStream转换XMLJSON

一. 简单介绍

xStream能够非常easy实现Java对象和xml文档互相转换, 能够改动某个特定的属性和节点名称。xStream提供annotation注解。

能够在JavaBean中完毕对xml节点和属性的描写叙述,并支持Json的转换。仅仅须要提供相关的JSONDriver就能完毕转换

官方站点: http://xstream.codehaus.org/tutorial.html


二. 准备工作

1. 环境准备:

Jar文件下载地址:

https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/xstream-distribution/1.3.1/xstream-distribution-1.3.1-bin.zip

代码结构图:

技术分享


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("[email protected]");
		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>[email protected]</email>
  <address>china</address>
  <birthday>
    <birthday>2010-11-22</birthday>
  </birthday>
</com.zdp.domain.Student>
<!-- 重命名后的XML -->
<student>
  <id>1</id>
  <name>jack</name>
  <邮件>[email protected]</邮件>
  <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("[email protected]");
			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>[email protected]</email>
      <address>china</address>
      <birthday id="4" 生日="2010-11-22"/>
    </student>
    <student reference="3"/>
    <student id="5" 姓名="tom">
      <id>2</id>
      <email>[email protected]</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>[email protected]</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("[email protected]");
			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>[email protected]</email>
      <address>china</address>
      <birthday birthday="2010-11-22"/>
    </student>
  </entry>
  <entry>
    <key>No.2</key>
    <student id="2">
      <name>tom</name>
      <email>[email protected]</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
[email protected]
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("[email protected]");
	        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#[email protected]#[email protected]
====== XML >>> List ======
size:3
jack#1#china#[email protected]#[email protected]
tom#2#china#[email protected]#[email protected]
jack#0#null#null#null
====== XML >>> Map ======
size:3
No.3:jack#0#null#null#null
No.1:jack#1#china#[email protected]#[email protected]
No.2:tom#2#china#[email protected]#[email protected]

五. 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":"[email protected]","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": "[email protected]",
  "address": "china",
  "birthday": {
    "birthday": "2010-11-22"
  }
}}
{
  "id": 1,
  "name": "jack",
  "email": "[email protected]",
  "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("[email protected]");
	    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": "[email protected]",
    "address": "china",
    "birthday": {
      "birthday": "2010-11-22"
    }
  },
  {
    "id": 2,
    "name": "tom",
    "email": "[email protected]",
    "address": "china",
    "birthday": {
      "birthday": "2010-11-22"
    }
  },
  {
    "id": 0,
    "name": "jack"
  }
]}
[
  {
    "id": 1,
    "name": "jack",
    "email": "[email protected]",
    "address": "china",
    "birthday": {
      "birthday": "2010-11-22"
    }
  },
  {
    "id": 2,
    "name": "tom",
    "email": "[email protected]",
    "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("[email protected]");
	    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": "[email protected]",
      "address": "china",
      "birthday": {
        "birthday": "2010-11-22"
      }
    }
  ],
  [
    "No.2",
    {
      "id": 2,
      "name": "tom",
      "email": "[email protected]",
      "address": "china",
      "birthday": {
        "birthday": "2010-11-21"
      }
    }
  ]
]}
[
  [
    "No.3",
    {
      "id": 0,
      "name": "jack"
    }
  ],
  [
    "No.1",
    {
      "id": 1,
      "name": "jack",
      "email": "[email protected]",
      "address": "china",
      "birthday": {
        "birthday": "2010-11-22"
      }
    }
  ],
  [
    "No.2",
    {
      "id": 2,
      "name": "tom",
      "email": "[email protected]",
      "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: [email protected]," +
		            "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#[email protected]#email
{list: [{id: 1,name: haha,email: email,address: address,birthday: {birthday: 2010-11-22}},{id: 2,name: tom,email: [email protected],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转化java时,节点大小写转化的主要内容,如果未能解决你的问题,请参考以下文章

xstream xml转化成java对象

XStream将java对象解析为xml字符串时,过滤掉节点值为空的节点

怎么将json文件转成xml文件

xStream完美转换XMLJSON(转)

xstream如何将java对象转化成有cdata的xml

Java 对象转XML xStream 附下载方式