deserializing xml file from xstream

狂风中的少年 提交于 2019-12-01 09:41:00
Adriaan Koster

fromXML does not take a filename, try:

File xmlFile = new File("model.xml");
xstream.fromXML(new FileInputStream(xmlFile));

to read the file contents as a String.

Also the fieldnames 'id' and 'reference' happen to be 'system attributes' in XStream. Using the following code:

CarImpl myModel = new CarImpl();

File xmlFile = new File("model.xml");

XStream xstream = new XStream();
xstream.useAttributeFor(String.class);
xstream.useAttributeFor(Integer.class);

Writer writer = new FileWriter(xmlFile);        
writer.write(xstream.toXML(myModel));
writer.close();

CarImpl fromXML = (CarImpl) xstream.fromXML(new FileInputStream(xmlFile));
System.out.println(fromXML);

unmarshalling fails if the fields are called 'id' and 'reference', but succeeds otherwise. See XStream FAQ

Take a look at the new method 'aliasForSystemAttribute' for a possible solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!