xstream

Xstream之常用方式与常用注解

梦想与她 提交于 2019-12-05 23:16:30
参考资料 1 xStream框架完美实现Java对象和xml文档JSON、XML相互转换 http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html 2 xStream完美转换XML、JSON http://archive.cnblogs.com/a/2025197/ 官网:http://xstream.codehaus.org/download.html 相关jar包可在: XStream之初识 http://liuzidong.iteye.com/blog/1059453 下载 示例代码 Java代码 Blog teamBlog = new Blog( new Author( "Guilherme Silveira" )); teamBlog.add( new Entry( "first" , "My first blog entry." )); eamBlog.add( new Entry( "tutorial" , "Today we have developed a nice alias tutorial. Tell your friends! NOW!" )); XStream xstream = new XStream(); System.out.println(xstream.toXML(teamBlog

How to map an Hashmap to key-value-attributes in XML using xstream

回眸只為那壹抹淺笑 提交于 2019-12-05 19:26:27
I have the following entity: @XStreamAlias("entity") public class MapTestEntity { @XStreamAsAttribute public Map<String, String> myMap = new HashMap<>(); @XStreamAsAttribute public String myText; } I use it with xstream like: MapTestEntity e = new MapTestEntity(); e.myText = "Foo"; e.myMap.put("firstname", "homer"); e.myMap.put("lastname", "simpson"); XStream xstream = new XStream(new PureJavaReflectionProvider()); xstream.processAnnotations(MapTestEntity.class); System.out.println(xstream.toXML(e)); and get the following xml: <entity myText="Foo"> <myMap> <entry> <string>lastname</string>

xstream errors for serializing & deserializing

≯℡__Kan透↙ 提交于 2019-12-05 09:02:48
I am using xStream in Java to serialize a java object from a java library and deserializing it at the customer's side. I have several problems: If I do it like this: XStream xstream = new XStream(); xstream.setMode(XStream.ID_REFERENCES); xstream.autodetectAnnotations(true); Writer writer = new FileWriter(xmlFile); writer.write(xstream.toXML(myObject)); writer.close(); => serializing is OK but deserializing: Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen .... @1:1) if I do it

Proguard and XStream with omitField() on Android

泪湿孤枕 提交于 2019-12-05 07:20:50
问题 I was using XStream for deserialization of xml in my Android app, and now I'm struggling to add Proguard (obfuscator) to the mix. Here's the runtime exception I run into (full: pastebin): WARN/System.err(6209): net.lp.collectionista.util.a.g: XStream could not parse the response WARN/System.err(6209): at net.lp.collectionista.a.s.a(Collectionista:215) ... WARN/System.err(6209): Caused by: com.thoughtworks.xstream.converters.ConversionException: id : id in loader dalvik.system.PathClassLoader[

XStream: Collapsing XML hierarchy as I parse

北城余情 提交于 2019-12-05 07:15:28
I have an XML document (generated by Adobe XFA forms), that contains data like the following: <Position> <PositionBorder> <Title/> <StartDate/> <EndDate/> </PositionBorder> </Position> Since this file is defined elsewhere, I am not at liberty to change the format of the XML that I get. In my Java code, I create a Position class that contains the Title, Start and End Dates. My problem is, when I use XStream to parse the file, it wants a PositionBorder class to hold the title and dates. I want to basically ignore the border and place all of the fields into the Position class. What I'd really

Make XStream ignore one specific private variable

梦想的初衷 提交于 2019-12-05 04:57:45
I have a little problem with a class I am currently writing a save function for. I'm using XStream (com.thoughtworks.xstream) to serialize a class to XML using the DOMDriver. The class looks like this: public class World { private Configuration config; public World(Configuration config) { this.config = config; } } So, the issue here is that I do not want to serialize Configuration when serializing world, rather I'd like to give XStream a preconstructed Configuration instance when calling fromXml(). Problem here is mainly class design, Configuration holds a private reference to the GUI classes

Set default value for fields not in XML in XStream

让人想犯罪 __ 提交于 2019-12-04 19:04:54
问题 Is there a way to create a converter or some operation that is performed after every single conversion? For context, I am trying to populate default values for fields that are not in my XML in order to maintain backwards compatibility if my data model changes. For instance, if I had this object: class A { private String b; private String c; private String d; } and my XML was something like: <a> <b>b</b> <d>d</d> </a> I want my import of the XML to know that there is a default value for the

android下通过xstream解析复杂的xml格式信息

纵饮孤独 提交于 2019-12-04 13:02:08
在 android下通过xstream解析xml格式信息 一文中介绍了通过xstream解析比较简单的xml文件到相应的Java bean中,下面介绍带节点的xml文件,xml文件格式如下 <?xml version="1.0" encoding="utf-8"?> <city id="1"> <name> <item lang="en"><value>ShangHai</value></item> <item lang="zh"><value>上海</value></item> </name> <content> <item lang="en"><value>ShangHai ...</value></item> <item lang="zh"><value>上海...</value></item> </content> <images> <item name="title" src="http://mp.myvsp.cn/images/shanghai.png"/> </images> </city> 解析的代码如下: 1、先创建相应的java bean: public class CityInfo { private String id; private List<ItemInfo> name=new ArrayList<ItemInfo>(); private List

Polymorphism in XStream serialization and deserialization

…衆ロ難τιáo~ 提交于 2019-12-04 11:57:55
I have these classes: @XStreamAlias("person") public class PersonConfig { private AnimalConfig animalConfig; } public interface AnimalConfig {} @XStreamAlias("dog"); public class DogConfig extend AnimalConfig {} @XStreamAlias("cat"); public class CatConfig extend AnimalConfig {} And I would like to be able to deserialize this xml with the classes above: <person> <dog/> <person> As well as deserialize this xml too, with the same classes: <person> <cat/> <person> So that in both cases, the PersonConfig 's field animalConfig is filled. In the first XML with a DogConfig instance and in the second

Can't add xstream 1.4.8 dependency to Android using gradle

こ雲淡風輕ζ 提交于 2019-12-04 10:40:00
I'm having difficulties with including xstream library to my Android gradle-based app. From what I've read in xstream documentation, it should work "out of the box". However, when I add dependency: compile 'com.thoughtworks.xstream:xstream:1.4.8' I get following exception during build process: * What went wrong: Execution failed for task ':app:transformClassesWithJarMergingForDebug'. > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/xmlpull/v1/XmlPullParser.class Ok, so maybe I should exclude xmlpull? I tried changing this dependency to: