- 完成java object和xml之间的转换方法有多种。一种是利用xtream,一种是利用hibernate的第三方工具。
- xtream非常简单,不需要生成dtd,无用配置,不需要生成辅助类,虽然功能相对其他的同类工具要简单,但是基本上都能满足需要。
- hibernate有第三方工具,可以方便的在xml/obj/db之间转换。
- xtream+xpp也是一个很好的选择
- public static void write() {
- XStream sm = new XStream();
- mytest t = new mytest();
- t.setName("moogle");
- t.setXb("男");
- try {
- FileOutputStream ops = new FileOutputStream(new File("C:\111.xml"));
- sm.toXML(t, ops);
- ops.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void read() {
- XStream sm = new XStream(new DomDriver());
- try {
- FileInputStream ops = new FileInputStream(new File("C:\111.xml"));
- mytest t = (mytest)sm.fromXML(ops);
- System.out.println(t.getName());
- ops.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
生成的XML文件内容:
问题:
1.生成的xml文档没有<!---->
2.如果生成的文档中含有中文,比如上文代码中setXb("男")
在读取的时候会报
[Fatal Error] :4:7: Invalid byte 2 of 2-byte UTF-8 sequence.
解决办法
-
加入xpp3_min-1.1.3.4.O.jar(包含在xStream压缩包中)
将
XStream sm = new XStream(new DomDriver());XStream sm = new XStream();
不需要生成dtd,无用配置,不需要生成辅助类,速度快。这就是xstream+xpp超强黄金组合。
xstream大家都知道啦,XML Pull Parser是一种高速的 解析xml文件的方式,速度要比传统方式快很多(发现pull式解析现在比较流行了)。下面我给出多种使用方法的例子。
1.最简单的使用方法
因为这个太简单,所以我从moogle的blog http://moogle.javaeye.com/blog/34661取下面的例子
1. public static void write() {
2. XStream sm = new XStream();
3. mytest t = new mytest();
4. t.setName("moogle");
5. t.setXb("男");
6. try {
7. FileOutputStream ops = new FileOutputStream(new File("C://111.xml"));
8. sm.toXML(t, ops);
9. ops.close();
10. } catch (Exception e) {
11. e.printStackTrace();
12. }
13. }
14. public static void read() {
15. XStream sm = new XStream(new DomDriver());
16. try {
17. FileInputStream ops = new FileInputStream(new File("C://111.xml"));
18. mytest t = (mytest)sm.fromXML(ops);
19. System.out.println(t.getName());
20. ops.close();
21. } catch (Exception e) {
22. e.printStackTrace();
23. }
24. }
生成 XML是
# <mytest>
# <name>asd</name>
# <xb>男</xb>
2.中等方法(需要1.2版以上才有这个功能)
XStream stream = new XStream();
stream.alias("schema", SchemaObject.class);
stream.useAttributeFor("url", String.class);
stream.useAttributeFor("jdbcDriverClass", String.class);
stream.useAttributeFor("user", String.class);
stream.useAttributeFor("password", String.class);
FileOutputStream s = new FileOutputStream(file);
stream.toXML(theObject, s);
s.close();
alias和useAttributeFor是用作把 <com.hongsoft.model.SchemaObject>修改为<schema>
3.高级方法
XStream stream = new XStream();
stream.registerConverter(new SchemaXMLConvertor());
stream.alias("schema", SchemaObject.class);
FileInputStream s = new FileInputStream(file);
object = (SchemaObject) stream.fromXML(s);
s.close();
registerConverter可以实现把任何schema的XML和object互相转换,这个其实已经否定了很多朋友说的
“xstream+xpp”功能不强的说法。SchemaXMLConvertor示例如下:
public void marshal(Object arg0, HierarchicalStreamWriter writer,
MarshallingContext arg2) {
SchemaObject schema=(SchemaObject)arg0;
writer.startNode(SchemaObject.TABLE);
writer.addAttribute(SchemaObject.TABLE_NAME, iTable.getName());
//line
List<SchemaObject.TableObject.LineObject> lines=iTable.getLines();
for(int j=0;j<lines.size();j++)
{
SchemaObject.TableObject.LineObject jLine=lines.get(j);
writer.startNode(SchemaObject.LINE);
//column
Map<String,String> columns=jLine.getColumns();
Iterator ite=columns.keySet().iterator();
while(ite.hasNext())
{
String columnName=ite.next().toString();
writer.addAttribute(columnName, columns.get(columnName));
}
writer.endNode();
}
writer.endNode();
writer.underlyingWriter();
}
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext arg1) {
SchemaObject schema=new SchemaObject();
schema.setJdbcDriverClass(reader.getAttribute(SchemaObject.JDBC_DRIVER_CLASS));
schema.setUrl(reader.getAttribute(SchemaObject.URL));
schema.setUser(reader.getAttribute(SchemaObject.USER));
schema.setPassword(reader.getAttribute(SchemaObject.PASSWORD));
List<TableObject> tables = new ArrayList<TableObject>();
while(reader.hasMoreChildren()) {
reader.moveDown();
SchemaObject.TableObject table=new SchemaObject().new TableObject();
table.setName(reader.getAttribute(SchemaObject.TABLE_NAME));
tables.add(table);
reader.moveUp();
}
schema.setTables(tables);
return schema;
}
public boolean canConvert(Class arg0) {
return arg0.equals(SchemaObject.class);
}