xstream

XStream转换时忽略未知字段

冷暖自知 提交于 2019-12-02 00:59:28
XStream是一个用于将对象序列化为xml以及将xml生成对象的库,通过它可以非常方便的做对象与xml(json等其他格式)之间的转换。但是在使用XStream过程中,会遇到xml元素与对象元素并不是一一匹配的情况,而导致程序抛出异常。通过以下方法可以解决这个问题: XML转为对象时XML的要素比对象多 public class LetterHolder { private String A; private String B; // getters and setters ... } <LetterHolder> <A>This is letter A</A> <B>B</B> <C>C</C> </LetterHolder> 这时候我们可以使用这两个函数,告诉XStream要忽略掉未知的XML要素 ignoreUnknownElements(); ignoreUnknownElements(String pattern); 注意:XStream老版本可能没有这个函数(应该是1.4.5以上版本) 对象转为XML时想忽略掉对象中的某些元素 public class LetterHolder { private String A; private String B; private String C; // getters and setters ... }

JavaBean到XML和JSON的转换

杀马特。学长 韩版系。学妹 提交于 2019-12-02 00:59:15
XML和JSON是当今常用的两种数据描述与传输的格式,特别是涉及到JS时使用JSON颇为频繁。自然,在Java的世界里少不了完成JavaBean与这两种格式相互转换的组件,那就是XStream和JSON-lib。这里我简单记下XStream的用法。 其实相类似的工具早已有之。如果用过DWR的同志,一定有印像,DWR进行远程方法调用时也能为你完成JavaBean和JSON格式的双向转换的,所依赖的是它的各种Converter。再要是对Struts1的细节有所注意的话,Struts1的ActionServlet在初始化struts-config.xml时是通过commons-digester来完成XML到JavaBean转换的。相应的Apache也有一个commons-betwixt实现了JavaBean到XML的生成。 而我这里要说的XStream把JavaBean到XML和JSON的转换实现了,而JSON-lib则如其名,功能太显简陋了。要使用XStream,需下载到xstream包,当前版本是1.3.1。然后把xstream-1.x.x.jar添加到项目的Classpath上,可不依赖于其他包。在某些有要求时候才需要用到lib目录中的其他包,下面会提到。 简单说明XStream的使用吧,分为JavaBean->XML、JavaBean->JSON两部分内容。在开始例子之前

XStream serialize null values

走远了吗. 提交于 2019-12-01 15:47:21
问题 Suppose I have class Student { String name; int age; String teacher; } Then : public class App1 { public static void main(String[] args) { Student st = new Student(); st.setName("toto"); XStream xs = new XStream(); xs.alias("student",Student.class); System.out.println(xs.toXML(st)); } } Gives me : <student> <name>toto</name> <age>0</age> </student> Is there a way for dealing null values ? I mean : <student> <name>toto</name> <age>0</age> <teacher></teacher> </student> It's possible if I do st

Spring3 MVC Restful 多请求类型(json,xml,k-v),多视图配置(J...

假如想象 提交于 2019-12-01 15:36:52
beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema

Xstream Implicit Map As Attributes to Root Element

a 夏天 提交于 2019-12-01 13:21:27
I am trying to find a solution to convert a map into root element attributes using XStream. I do not think this is possible but here is what I have tried to far. I created a custom converter and attached it to the root object, in the converter I then get access to the map that I am trying to convert into attributes, I iterate through the map and write the attirbute to the node, using writer.addAttribute(entry.getKey(), entry.getValue()); this does actually write the attributes to the root node e.g. The problem with this approach is that it does not process the rest of the document, it just

get xml as string instead of class with xstream

被刻印的时光 ゝ 提交于 2019-12-01 11:29:26
I have xml something like <parent> <message> <type>15</type> </message> </parent> Instead of creating a message object inside parent object, I need to represent message just as a String . So , when I do parent.message , the output is <type> 15 </type> instead of a message object. The idia is to build up the xml of message by processing the HierarchicalStreamReader . If you go down into <messsage> by calling reader.goDown() unfortunately reader.getValue() does not return the whole content of this element. Model @XStreamAlias("parent") @XStreamConverter(value = ParentConverter.class) public

Serialization problem with Enums at Android

 ̄綄美尐妖づ 提交于 2019-12-01 10:53:25
问题 I'm using XStream to serialize some objects to XML, and am facing a problem with Enums. The exception I get when I try to serialize the object: "ObjectAccessException: invalid final field java.lang.Enum.name". Apparently, this is a problem with the reflection API implementation in android: It doesn't treat final fields correctly. This problem actually existed in past implementations of the official Sun (Oracle) JDK. Can you confirm/refute this is the problem with Android? Can you suggest any

deserializing xml file from xstream

狂风中的少年 提交于 2019-12-01 09:41:00
I am using Xstream to serializing a Job object. It looks working fine. but deserializing, I have a problem: Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen .... @1:1) at com.thoughtworks.xstream.io.xml.XppReader.pullNextEvent(XppReader.java:78) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readRealEvent(AbstractPullReader.java:137) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readEvent(AbstractPullReader.java:130) at com.thoughtworks.xstream.io.xml

deserializing xml file from xstream

匆匆过客 提交于 2019-12-01 09:19:41
问题 I am using Xstream to serializing a Job object. It looks working fine. but deserializing, I have a problem: Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen .... @1:1) at com.thoughtworks.xstream.io.xml.XppReader.pullNextEvent(XppReader.java:78) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readRealEvent(AbstractPullReader.java:137) at com.thoughtworks.xstream.io.xml

How to read list elements with attribute via XStream

烂漫一生 提交于 2019-12-01 09:12:06
I'm using XStream to read below example xml file. <list> <file>/setup/x86-linux2/bin/zip.txt</file> <file type="dir">/src/bin/</file> <name>test xml</name> </list> Below is my code for reading above xml, public class ListWithConverter { public static class FileConvertor implements Converter { public boolean canConvert(final Class clazz) { return clazz.equals(MyFile.class); } public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { throw new UnsupportedOperationException("Not supported to write file element yet."); //$NON-NLS-1$ } public Object unmarshal