http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/XStream.html#ignoreUnknownElements%28%29
Class XStream
- java.lang.Object
-
- com.thoughtworks.xstream.XStream
-
public class XStream extends Object
Simple facade to XStream library, a Java-XML serialization tool.
ExampleXStream xstream = new XStream(); String xml = xstream.toXML(myObject); // serialize to XML Object myObject2 = xstream.fromXML(xml); // deserialize from XML
Aliasing classes
To create shorter XML, you can specify aliases for classes using the
alias()
method. For example, you can shorten all occurrences of element<com.blah.MyThing>
to<my-thing>
by registering an alias for the class.
xstream.alias("my-thing", MyThing.class);
Converters
XStream contains a map of
Converter
instances, each of which acts as a strategy for converting a particular type of class to XML and back again. Out of the box, XStream contains converters for most basic types (String, Date, int, boolean, etc) and collections (Map, List, Set, Properties, etc). For other objects reflection is used to serialize each field recursively.Extra converters can be registered using the
registerConverter()
method. Some non-standard converters are supplied in thecom.thoughtworks.xstream.converters.extended
package and you can create your own by implementing theConverter
interface.
Examplexstream.registerConverter(new SqlTimestampConverter()); xstream.registerConverter(new DynamicProxyConverter());
The converters can be registered with an explicit priority. By default they are registered with XStream.PRIORITY_NORMAL. Converters of same priority will be used in the reverse sequence they have been registered. The default converter, i.e. the converter which will be used if no other registered converter is suitable, can be registered with priority XStream.PRIORITY_VERY_LOW. XStream uses by default the
ReflectionConverter
as the fallback converter.
Examplexstream.registerConverter(new CustomDefaultConverter(), XStream.PRIORITY_VERY_LOW);
Object graphs
XStream has support for object graphs; a deserialized object graph will keep references intact, including circular references.
XStream can signify references in XML using either relative/absolute XPath or IDs. The mode can be changed using
setMode()
:xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
(Default) Uses XPath relative references to signify duplicate references. This produces XML with the least clutter. xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
Uses XPath absolute references to signify duplicate references. This produces XML with the least clutter. xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES);
Uses XPath relative references to signify duplicate references. The XPath expression ensures that a single node only is selected always. xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES);
Uses XPath absolute references to signify duplicate references. The XPath expression ensures that a single node only is selected always. xstream.setMode(XStream.ID_REFERENCES);
Uses ID references to signify duplicate references. In some scenarios, such as when using hand-written XML, this is easier to work with. xstream.setMode(XStream.NO_REFERENCES);
This disables object graph support and treats the object structure like a tree. Duplicate references are treated as two separate objects and circular references cause an exception. This is slightly faster and uses less memory than the other two modes. Thread safety
The XStream instance is thread-safe. That is, once the XStream instance has been created and configured, it may be shared across multiple threads allowing objects to be serialized/deserialized concurrently. Note, that this only applies if annotations are not auto-detected on-the-fly.
Implicit collections
To avoid the need for special tags for collections, you can define implicit collections using one of the
addImplicitCollection
methods.- Author:
- Joe Walnes, Jörg Schaible, Mauro Talevi, Guilherme Silveira
-
-
Nested Class Summary
Nested Classes
Modifier and Type Class and Description static class
XStream.InitializationException
Deprecated.As of 1.3, useInitializationException
instead
-
Field Summary
Fields
Modifier and Type Field and Description static int
ID_REFERENCES
static int
NO_REFERENCES
static int
PRIORITY_LOW
static int
PRIORITY_NORMAL
static int
PRIORITY_VERY_HIGH
static int
PRIORITY_VERY_LOW
static int
SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES
static int
SINGLE_NODE_XPATH_RELATIVE_REFERENCES
static int
XPATH_ABSOLUTE_REFERENCES
static int
XPATH_RELATIVE_REFERENCES
-
Constructor Summary
-
Method Summary
All MethodsInstance MethodsConcrete MethodsDeprecated Methods
Modifier and Type Method and Description void
addDefaultImplementation(Class defaultImplementation, Class ofType)
Associate a default implementation of a class with an object.void
addImmutableType(Class type)
Add immutable types.void
addImplicitArray(Class ownerType, String fieldName)
Adds an implicit array.void
addImplicitArray(Class ownerType, String fieldName, Class itemType)
Adds an implicit array which is used for all items of the given itemType when the array type matches.void
addImplicitArray(Class ownerType, String fieldName, String itemName)
Adds an implicit array which is used for all items of the given element name defined by itemName.void
addImplicitCollection(Class ownerType, String fieldName)
Adds a default implicit collection which is used for any unmapped XML tag.void
addImplicitCollection(Class ownerType, String fieldName, Class itemType)
Adds implicit collection which is used for all items of the given itemType.void
addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType)
Adds implicit collection which is used for all items of the given element name defined by itemFieldName.void
addImplicitMap(Class ownerType, String fieldName, Class itemType, String keyFieldName)
Adds an implicit map.void
addImplicitMap(Class ownerType, String fieldName, String itemName, Class itemType, String keyFieldName)
Adds an implicit map.void
addPermission(TypePermission permission)
Add a new security permission.void
alias(String name, Class type)
Alias a Class to a shorter name to be used in XML elements.void
alias(String name, Class type, Class defaultImplementation)
Alias a Class to a shorter name to be used in XML elements.void
aliasAttribute(Class definedIn, String attributeName, String alias)
Create an alias for an attribute.void
aliasAttribute(String alias, String attributeName)
Create an alias for an attributevoid
aliasField(String alias, Class definedIn, String fieldName)
Create an alias for a field name.void
aliasPackage(String name, String pkgName)
Alias a package to a shorter name to be used in XML elements.void
aliasSystemAttribute(String alias, String systemAttributeName)
Create an alias for a system attribute.void
aliasType(String name, Class type)
Alias a type to a shorter name to be used in XML elements.void
allowTypeHierarchy(Class type)
Add security permission for a type hierarchy.void
allowTypes(Class[] types)
Add security permission for explicit types.void
allowTypes(String[] names)
Add security permission for explicit types by name.void
allowTypesByRegExp(Pattern[] regexps)
Add security permission for types matching one of the specified regular expressions.void
allowTypesByRegExp(String[] regexps)
Add security permission for types matching one of the specified regular expressions.void
allowTypesByWildcard(String[] patterns)
Add security permission for types matching one of the specified wildcard patterns.void
autodetectAnnotations(boolean mode)
Set the auto-detection mode of the AnnotationMapper.ObjectInputStream
createObjectInputStream(HierarchicalStreamReader reader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.ObjectInputStream
createObjectInputStream(InputStream in)
Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using XStream.ObjectInputStream
createObjectInputStream(Reader xmlReader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.ObjectOutputStream
createObjectOutputStream(HierarchicalStreamWriter writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.ObjectOutputStream
createObjectOutputStream(HierarchicalStreamWriter writer, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.ObjectOutputStream
createObjectOutputStream(OutputStream out)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.ObjectOutputStream
createObjectOutputStream(OutputStream out, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.ObjectOutputStream
createObjectOutputStream(Writer writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.ObjectOutputStream
createObjectOutputStream(Writer writer, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.void
denyPermission(TypePermission permission)
Add security permission denying another one.void
denyTypeHierarchy(Class type)
Add security permission forbidding a type hierarchy.void
denyTypes(Class[] types)
Add security permission forbidding explicit types.void
denyTypes(String[] names)
Add security permission forbidding explicit types by name.void
denyTypesByRegExp(Pattern[] regexps)
Add security permission forbidding types matching one of the specified regular expressions.void
denyTypesByRegExp(String[] regexps)
Add security permission forbidding types matching one of the specified regular expressions.void
denyTypesByWildcard(String[] patterns)
Add security permission forbidding types matching one of the specified wildcard patterns.Object
fromXML(File file)
Deserialize an object from a file.Object
fromXML(File file, Object root)
Deserialize an object from a file, populating the fields of the given root object instead of instantiating a new one.Object
fromXML(InputStream input)
Deserialize an object from an XML InputStream.Object
fromXML(InputStream input, Object root)
Deserialize an object from an XML InputStream, populating the fields of the given root object instead of instantiating a new one.Object
fromXML(Reader reader)
Deserialize an object from an XML Reader.Object
fromXML(Reader xml, Object root)
Deserialize an object from an XML Reader, populating the fields of the given root object instead of instantiating a new one.Object
fromXML(String xml)
Deserialize an object from an XML String.Object
fromXML(String xml, Object root)
Deserialize an object from an XML String, populating the fields of the given root object instead of instantiating a new one.Object
fromXML(URL url)
Deserialize an object from a URL.Object
fromXML(URL url, Object root)
Deserialize an object from a URL, populating the fields of the given root object instead of instantiating a new one.ClassLoader
getClassLoader()
Retrieve the ClassLoader XStream uses to load classes.ClassLoaderReference
getClassLoaderReference()
Retrieve the reference to this instance' ClassLoader.ConverterLookup
getConverterLookup()
Mapper
getMapper()
Retrieve theMapper
.ReflectionProvider
getReflectionProvider()
Retrieve theReflectionProvider
in use.void
ignoreUnknownElements()
Ignore all unknown elements.void
ignoreUnknownElements(Pattern pattern)
Add pattern for unknown element names to ignore.void
ignoreUnknownElements(String pattern)
Add pattern for unknown element names to ignore.void
marshal(Object obj, HierarchicalStreamWriter writer)
Serialize and object to a hierarchical data structure (such as XML).void
marshal(Object obj, HierarchicalStreamWriter writer, DataHolder dataHolder)
Serialize and object to a hierarchical data structure (such as XML).DataHolder
newDataHolder()
Create a DataHolder that can be used to pass data to the converters.void
omitField(Class definedIn, String fieldName)
Prevents a field from being serialized.void
processAnnotations(Class type)
Process the annotations of the given type and configure the XStream.void
processAnnotations(Class[] types)
Process the annotations of the given types and configure the XStream.void
registerConverter(Converter converter)
void
registerConverter(Converter converter, int priority)
void
registerConverter(SingleValueConverter converter)
void
registerConverter(SingleValueConverter converter, int priority)
void
registerLocalConverter(Class definedIn, String fieldName, Converter converter)
Register a localConverter
for a field.void
registerLocalConverter(Class definedIn, String fieldName, SingleValueConverter converter)
Register a localSingleValueConverter
for a field.void
setClassLoader(ClassLoader classLoader)
Change the ClassLoader XStream uses to load classes.void
setMarshallingStrategy(MarshallingStrategy marshallingStrategy)
void
setMode(int mode)
Change mode for dealing with duplicate references.protected void
setupAliases()
protected void
setupConverters()
protected void
setupDefaultImplementations()
protected void
setupImmutableTypes()
protected void
setupSecurity()
String
toXML(Object obj)
Serialize an object to a pretty-printed XML String.void
toXML(Object obj, OutputStream out)
Serialize an object to the given OutputStream as pretty-printed XML.void
toXML(Object obj, Writer out)
Serialize an object to the given Writer as pretty-printed XML.Object
unmarshal(HierarchicalStreamReader reader)
Deserialize an object from a hierarchical data structure (such as XML).Object
unmarshal(HierarchicalStreamReader reader, Object root)
Deserialize an object from a hierarchical data structure (such as XML), populating the fields of the given root object instead of instantiating a new one.Object
unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder)
Deserialize an object from a hierarchical data structure (such as XML).void
useAttributeFor(Class type)
Use an attribute for an arbitrary type.void
useAttributeFor(Class definedIn, String fieldName)
Use an attribute for a field declared in a specific type.void
useAttributeFor(String fieldName, Class type)
Use an attribute for a field or a specific type.protected boolean
useXStream11XmlFriendlyMapper()
Deprecated.As of 1.4.8protected MapperWrapper
wrapMapper(MapperWrapper next)
-
-
-
Field Detail
-
NO_REFERENCES
public static final int NO_REFERENCES
- See Also:
- Constant Field Values
-
ID_REFERENCES
public static final int ID_REFERENCES
- See Also:
- Constant Field Values
-
XPATH_RELATIVE_REFERENCES
public static final int XPATH_RELATIVE_REFERENCES
- See Also:
- Constant Field Values
-
XPATH_ABSOLUTE_REFERENCES
public static final int XPATH_ABSOLUTE_REFERENCES
- See Also:
- Constant Field Values
-
SINGLE_NODE_XPATH_RELATIVE_REFERENCES
public static final int SINGLE_NODE_XPATH_RELATIVE_REFERENCES
- See Also:
- Constant Field Values
-
SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES
public static final int SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES
- See Also:
- Constant Field Values
-
PRIORITY_VERY_HIGH
public static final int PRIORITY_VERY_HIGH
- See Also:
- Constant Field Values
-
PRIORITY_NORMAL
public static final int PRIORITY_NORMAL
- See Also:
- Constant Field Values
-
PRIORITY_LOW
public static final int PRIORITY_LOW
- See Also:
- Constant Field Values
-
PRIORITY_VERY_LOW
public static final int PRIORITY_VERY_LOW
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
XStream
public XStream()
Constructs a default XStream.The instance will use the
XppDriver
as default and tries to determine the best match for theReflectionProvider
on its own.- Throws:
XStream.InitializationException
- in case of an initialization problem
-
XStream
public XStream(ReflectionProvider reflectionProvider)
Constructs an XStream with a specialReflectionProvider
.The instance will use the
XppDriver
as default.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching reflection provider- Throws:
XStream.InitializationException
- in case of an initialization problem
-
XStream
public XStream(HierarchicalStreamDriver hierarchicalStreamDriver)
Constructs an XStream with a specialHierarchicalStreamDriver
.The instance will tries to determine the best match for the
ReflectionProvider
on its own.- Parameters:
hierarchicalStreamDriver
- the driver instance- Throws:
XStream.InitializationException
- in case of an initialization problem
-
XStream
public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver hierarchicalStreamDriver)
Constructs an XStream with a specialHierarchicalStreamDriver
andReflectionProvider
.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching ProviderhierarchicalStreamDriver
- the driver instance- Throws:
XStream.InitializationException
- in case of an initialization problem
-
XStream
public XStream(ReflectionProvider reflectionProvider, Mapper mapper, HierarchicalStreamDriver driver)
Deprecated. As of 1.3, useXStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoader, Mapper)
insteadConstructs an XStream with a specialHierarchicalStreamDriver
,ReflectionProvider
and a preparedMapper
chain.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching Providermapper
- the instance with theMapper
chain or null for the default chaindriver
- the driver instance- Throws:
XStream.InitializationException
- in case of an initialization problem
-
XStream
public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoaderReference)
Constructs an XStream with a specialHierarchicalStreamDriver
,ReflectionProvider
and aClassLoaderReference
.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoaderReference
- the reference to theClassLoader
to use- Throws:
XStream.InitializationException
- in case of an initialization problem- Since:
- 1.4.5
-
XStream
public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader)
Deprecated. As of 1.4.5 useXStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference)
Constructs an XStream with a specialHierarchicalStreamDriver
,ReflectionProvider
and theClassLoader
to use.- Throws:
XStream.InitializationException
- in case of an initialization problem- Since:
- 1.3
-
XStream
public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader, Mapper mapper)
Deprecated. As of 1.4.5 useXStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference, Mapper)
Constructs an XStream with a specialHierarchicalStreamDriver
,ReflectionProvider
, a preparedMapper
chain and theClassLoader
to use.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoader
- theClassLoader
to usemapper
- the instance with theMapper
chain or null for the default chain- Throws:
XStream.InitializationException
- in case of an initialization problem- Since:
- 1.3
-
XStream
public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoaderReference, Mapper mapper)
Constructs an XStream with a specialHierarchicalStreamDriver
,ReflectionProvider
, a preparedMapper
chain and theClassLoaderReference
.The
ClassLoaderReference
should also be used for theMapper
chain.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoaderReference
- the reference to theClassLoader
to usemapper
- the instance with theMapper
chain or null for the default chain- Throws:
XStream.InitializationException
- in case of an initialization problem- Since:
- 1.4.5
-
XStream
public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader, Mapper mapper, ConverterLookup converterLookup, ConverterRegistry converterRegistry)
Deprecated. As of 1.4.5 useXStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference, Mapper, ConverterLookup, ConverterRegistry)
Constructs an XStream with a specialHierarchicalStreamDriver
,ReflectionProvider
, a preparedMapper
chain, theClassLoaderReference
and an ownConverterLookup
andConverterRegistry
.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoader
- theClassLoader
to usemapper
- the instance with theMapper
chain or null for the default chainconverterLookup
- the instance that is used to lookup the convertersconverterRegistry
- an instance to manage the converter instances- Throws:
XStream.InitializationException
- in case of an initialization problem- Since:
- 1.3
-
XStream
public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoaderReference, Mapper mapper, ConverterLookup converterLookup, ConverterRegistry converterRegistry)
Constructs an XStream with a specialHierarchicalStreamDriver
,ReflectionProvider
, a preparedMapper
chain, theClassLoaderReference
and an ownConverterLookup
andConverterRegistry
.The ClassLoaderReference should also be used for the Mapper chain. The ConverterLookup should access the ConverterRegistry if you intent to register
Converter
instances with XStream facade or you are using annotations.- Parameters:
reflectionProvider
- the reflection provider to use or null for best matching Providerdriver
- the driver instanceclassLoaderReference
- the reference to theClassLoader
to usemapper
- the instance with theMapper
chain or null for the default chainconverterLookup
- the instance that is used to lookup the convertersconverterRegistry
- an instance to manage the converter instances or null to prevent any further registry (including annotations)- Throws:
XStream.InitializationException
- in case of an initialization problem- Since:
- 1.4.5
-
-
Method Detail
-
wrapMapper
protected MapperWrapper wrapMapper(MapperWrapper next)
-
useXStream11XmlFriendlyMapper
protected boolean useXStream11XmlFriendlyMapper()
Deprecated. As of 1.4.8
-
setupSecurity
protected void setupSecurity()
-
setupAliases
protected void setupAliases()
-
setupDefaultImplementations
protected void setupDefaultImplementations()
-
setupConverters
protected void setupConverters()
-
setupImmutableTypes
protected void setupImmutableTypes()
-
setMarshallingStrategy
public void setMarshallingStrategy(MarshallingStrategy marshallingStrategy)
-
toXML
public String toXML(Object obj)
Serialize an object to a pretty-printed XML String.- Throws:
XStreamException
- if the object cannot be serialized
-
toXML
public void toXML(Object obj, Writer out)
Serialize an object to the given Writer as pretty-printed XML. The Writer will be flushed afterwards and in case of an exception.- Throws:
XStreamException
- if the object cannot be serialized
-
toXML
public void toXML(Object obj, OutputStream out)
Serialize an object to the given OutputStream as pretty-printed XML. The OutputStream will be flushed afterwards and in case of an exception.- Throws:
XStreamException
- if the object cannot be serialized
-
marshal
public void marshal(Object obj, HierarchicalStreamWriter writer)
Serialize and object to a hierarchical data structure (such as XML).- Throws:
XStreamException
- if the object cannot be serialized
-
marshal
public void marshal(Object obj, HierarchicalStreamWriter writer, DataHolder dataHolder)
Serialize and object to a hierarchical data structure (such as XML).- Parameters:
dataHolder
- Extra data you can use to pass to your converters. Use this as you want. If not present, XStream shall create one lazily as needed.- Throws:
XStreamException
- if the object cannot be serialized
-
fromXML
public Object fromXML(String xml)
Deserialize an object from an XML String.- Throws:
XStreamException
- if the object cannot be deserialized
-
fromXML
public Object fromXML(Reader reader)
Deserialize an object from an XML Reader.- Throws:
XStreamException
- if the object cannot be deserialized
-
fromXML
public Object fromXML(InputStream input)
Deserialize an object from an XML InputStream.- Throws:
XStreamException
- if the object cannot be deserialized
-
fromXML
public Object fromXML(URL url)
Deserialize an object from a URL. Depending on the parser implementation, some might take the file path as SystemId to resolve additional references.- Throws:
XStreamException
- if the object cannot be deserialized- Since:
- 1.4
-
fromXML
public Object fromXML(File file)
Deserialize an object from a file. Depending on the parser implementation, some might take the file path as SystemId to resolve additional references.- Throws:
XStreamException
- if the object cannot be deserialized- Since:
- 1.4
-
fromXML
public Object fromXML(String xml, Object root)
Deserialize an object from an XML String, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!- Throws:
XStreamException
- if the object cannot be deserialized
-
fromXML
public Object fromXML(Reader xml, Object root)
Deserialize an object from an XML Reader, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!- Throws:
XStreamException
- if the object cannot be deserialized
-
fromXML
public Object fromXML(URL url, Object root)
Deserialize an object from a URL, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care! Depending on the parser implementation, some might take the file path as SystemId to resolve additional references.- Throws:
XStreamException
- if the object cannot be deserialized- Since:
- 1.4
-
fromXML
public Object fromXML(File file, Object root)
Deserialize an object from a file, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care! Depending on the parser implementation, some might take the file path as SystemId to resolve additional references.- Throws:
XStreamException
- if the object cannot be deserialized- Since:
- 1.4
-
fromXML
public Object fromXML(InputStream input, Object root)
Deserialize an object from an XML InputStream, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!- Throws:
XStreamException
- if the object cannot be deserialized
-
unmarshal
public Object unmarshal(HierarchicalStreamReader reader)
Deserialize an object from a hierarchical data structure (such as XML).- Throws:
XStreamException
- if the object cannot be deserialized
-
unmarshal
public Object unmarshal(HierarchicalStreamReader reader, Object root)
Deserialize an object from a hierarchical data structure (such as XML), populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!- Throws:
XStreamException
- if the object cannot be deserialized
-
unmarshal
public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder)
Deserialize an object from a hierarchical data structure (such as XML).- Parameters:
root
- If present, the passed in object will have its fields populated, as opposed to XStream creating a new instance. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!dataHolder
- Extra data you can use to pass to your converters. Use this as you want. If not present, XStream shall create one lazily as needed.- Throws:
XStreamException
- if the object cannot be deserialized
-
alias
public void alias(String name, Class type)
Alias a Class to a shorter name to be used in XML elements.- Parameters:
name
- Short nametype
- Type to be aliased- Throws:
XStream.InitializationException
- if noClassAliasingMapper
is available
-
aliasType
public void aliasType(String name, Class type)
Alias a type to a shorter name to be used in XML elements. Any class that is assignable to this type will be aliased to the same name.- Parameters:
name
- Short nametype
- Type to be aliased- Throws:
XStream.InitializationException
- if noClassAliasingMapper
is available- Since:
- 1.2
-
alias
public void alias(String name, Class type, Class defaultImplementation)
Alias a Class to a shorter name to be used in XML elements.- Parameters:
name
- Short nametype
- Type to be aliaseddefaultImplementation
- Default implementation of type to use if no other specified.- Throws:
XStream.InitializationException
- if noDefaultImplementationsMapper
or noClassAliasingMapper
is available
-
aliasPackage
public void aliasPackage(String name, String pkgName)
Alias a package to a shorter name to be used in XML elements.- Parameters:
name
- Short namepkgName
- package to be aliased- Throws:
XStream.InitializationException
- if noDefaultImplementationsMapper
or noPackageAliasingMapper
is available- Since:
- 1.3.1
-
aliasField
public void aliasField(String alias, Class definedIn, String fieldName)
Create an alias for a field name.- Parameters:
alias
- the alias itselfdefinedIn
- the type that declares the fieldfieldName
- the name of the field- Throws:
XStream.InitializationException
- if noFieldAliasingMapper
is available
-
aliasAttribute
public void aliasAttribute(String alias, String attributeName)
Create an alias for an attribute- Parameters:
alias
- the alias itselfattributeName
- the name of the attribute- Throws:
XStream.InitializationException
- if noAttributeAliasingMapper
is available
-
aliasSystemAttribute
public void aliasSystemAttribute(String alias, String systemAttributeName)
Create an alias for a system attribute. XStream will not write a system attribute if its alias is set tonull
. However, this is not reversible, i.e. deserialization of the result is likely to fail afterwards and will not produce an object equal to the originally written one.- Parameters:
alias
- the alias itself (may benull
)systemAttributeName
- the name of the system attribute- Throws:
XStream.InitializationException
- if noSystemAttributeAliasingMapper
is available- Since:
- 1.3.1
-
aliasAttribute
public void aliasAttribute(Class definedIn, String attributeName, String alias)
Create an alias for an attribute.- Parameters:
definedIn
- the type where the attribute is definedattributeName
- the name of the attributealias
- the alias itself- Throws:
XStream.InitializationException
- if noAttributeAliasingMapper
is available- Since:
- 1.2.2
-
useAttributeFor
public void useAttributeFor(String fieldName, Class type)
Use an attribute for a field or a specific type.- Parameters:
fieldName
- the name of the fieldtype
- the Class of the type to be rendered as XML attribute- Throws:
XStream.InitializationException
- if noAttributeMapper
is available- Since:
- 1.2
-
useAttributeFor
public void useAttributeFor(Class definedIn, String fieldName)
Use an attribute for a field declared in a specific type.- Parameters:
fieldName
- the name of the fielddefinedIn
- the Class containing such field- Throws:
XStream.InitializationException
- if noAttributeMapper
is available- Since:
- 1.2.2
-
useAttributeFor
public void useAttributeFor(Class type)
Use an attribute for an arbitrary type.- Parameters:
type
- the Class of the type to be rendered as XML attribute- Throws:
XStream.InitializationException
- if noAttributeMapper
is available- Since:
- 1.2
-
addDefaultImplementation
public void addDefaultImplementation(Class defaultImplementation, Class ofType)
Associate a default implementation of a class with an object. Whenever XStream encounters an instance of this type, it will use the default implementation instead. For example, java.util.ArrayList is the default implementation of java.util.List.- Parameters:
defaultImplementation
-ofType
-- Throws:
XStream.InitializationException
- if noDefaultImplementationsMapper
is available
-
addImmutableType
public void addImmutableType(Class type)
Add immutable types. The value of the instances of these types will always be written into the stream even if they appear multiple times.- Throws:
XStream.InitializationException
- if noImmutableTypesMapper
is available
-
registerConverter
public void registerConverter(Converter converter)
-
registerConverter
public void registerConverter(Converter converter, int priority)
-
registerConverter
public void registerConverter(SingleValueConverter converter)
-
registerConverter
public void registerConverter(SingleValueConverter converter, int priority)
-
registerLocalConverter
public void registerLocalConverter(Class definedIn, String fieldName, Converter converter)
Register a localConverter
for a field.- Parameters:
definedIn
- the class type the field is defined infieldName
- the field nameconverter
- the converter to use- Since:
- 1.3
-
registerLocalConverter
public void registerLocalConverter(Class definedIn, String fieldName, SingleValueConverter converter)
Register a localSingleValueConverter
for a field.- Parameters:
definedIn
- the class type the field is defined infieldName
- the field nameconverter
- the converter to use- Since:
- 1.3
-
getMapper
public Mapper getMapper()
Retrieve theMapper
. This is by default a chain ofMapperWrappers
.- Returns:
- the mapper
- Since:
- 1.2
-
getReflectionProvider
public ReflectionProvider getReflectionProvider()
Retrieve theReflectionProvider
in use.- Returns:
- the mapper
- Since:
- 1.2.1
-
getConverterLookup
public ConverterLookup getConverterLookup()
-
setMode
public void setMode(int mode)
Change mode for dealing with duplicate references. Valid values areXPATH_ABSOLUTE_REFERENCES
,XPATH_RELATIVE_REFERENCES
,XStream.ID_REFERENCES
andXStream.NO_REFERENCES
.- Throws:
IllegalArgumentException
- if the mode is not one of the declared types- See Also:
XPATH_ABSOLUTE_REFERENCES
,XPATH_RELATIVE_REFERENCES
,ID_REFERENCES
,NO_REFERENCES
-
addImplicitCollection
public void addImplicitCollection(Class ownerType, String fieldName)
Adds a default implicit collection which is used for any unmapped XML tag.- Parameters:
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This field must be a concrete collection type or matching the default implementation type of the collection type.
-
addImplicitCollection
public void addImplicitCollection(Class ownerType, String fieldName, Class itemType)
Adds implicit collection which is used for all items of the given itemType.- Parameters:
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This field must be a concrete collection type or matching the default implementation type of the collection type.itemType
- type of the items to be part of this collection- Throws:
XStream.InitializationException
- if noImplicitCollectionMapper
is available
-
addImplicitCollection
public void addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType)
Adds implicit collection which is used for all items of the given element name defined by itemFieldName.- Parameters:
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This field must be a concrete collection type or matching the default implementation type of the collection type.itemFieldName
- element name of the implicit collectionitemType
- item type to be aliases be the itemFieldName- Throws:
XStream.InitializationException
- if noImplicitCollectionMapper
is available
-
addImplicitArray
public void addImplicitArray(Class ownerType, String fieldName)
Adds an implicit array.- Parameters:
ownerType
- class owning the implicit arrayfieldName
- name of the array field- Since:
- 1.4
-
addImplicitArray
public void addImplicitArray(Class ownerType, String fieldName, Class itemType)
Adds an implicit array which is used for all items of the given itemType when the array type matches.- Parameters:
ownerType
- class owning the implicit arrayfieldName
- name of the array field in the ownerTypeitemType
- type of the items to be part of this array- Throws:
XStream.InitializationException
- if noImplicitCollectionMapper
is available or the array type does not match the itemType- Since:
- 1.4
-
addImplicitArray
public void addImplicitArray(Class ownerType, String fieldName, String itemName)
Adds an implicit array which is used for all items of the given element name defined by itemName.- Parameters:
ownerType
- class owning the implicit arrayfieldName
- name of the array field in the ownerTypeitemName
- alias name of the items- Throws:
XStream.InitializationException
- if noImplicitCollectionMapper
is available- Since:
- 1.4
-
addImplicitMap
public void addImplicitMap(Class ownerType, String fieldName, Class itemType, String keyFieldName)
Adds an implicit map.- Parameters:
ownerType
- class owning the implicit mapfieldName
- name of the field in the ownerType. This field must be a concrete map type or matching the default implementation type of the map type.itemType
- type of the items to be part of this map as valuekeyFieldName
- the name of the field of the itemType that is used for the key in the map- Since:
- 1.4
-
addImplicitMap
public void addImplicitMap(Class ownerType, String fieldName, String itemName, Class itemType, String keyFieldName)
Adds an implicit map.- Parameters:
ownerType
- class owning the implicit mapfieldName
- name of the field in the ownerType. This field must be a concrete map type or matching the default implementation type of the map type.itemName
- alias name of the itemsitemType
- type of the items to be part of this map as valuekeyFieldName
- the name of the field of the itemType that is used for the key in the map- Since:
- 1.4
-
newDataHolder
public DataHolder newDataHolder()
Create a DataHolder that can be used to pass data to the converters. The DataHolder is provided with a call tomarshal(Object, HierarchicalStreamWriter, DataHolder)
orunmarshal(HierarchicalStreamReader, Object, DataHolder)
.- Returns:
- a new
DataHolder
-
createObjectOutputStream
public ObjectOutputStream createObjectOutputStream(Writer writer) throws IOException
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.- Throws:
IOException
- Since:
- 1.0.3
- See Also:
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
-
createObjectOutputStream
public ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer) throws IOException
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.- Throws:
IOException
- Since:
- 1.0.3
- See Also:
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
-
createObjectOutputStream
public ObjectOutputStream createObjectOutputStream(Writer writer, String rootNodeName) throws IOException
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.- Throws:
IOException
- Since:
- 1.0.3
- See Also:
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
-
createObjectOutputStream
public ObjectOutputStream createObjectOutputStream(OutputStream out) throws IOException
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.- Throws:
IOException
- Since:
- 1.3
- See Also:
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
-
createObjectOutputStream
public ObjectOutputStream createObjectOutputStream(OutputStream out, String rootNodeName) throws IOException
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.- Throws:
IOException
- Since:
- 1.3
- See Also:
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
-
createObjectOutputStream
public ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer, String rootNodeName) throws IOException
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.Because an ObjectOutputStream can contain multiple items and XML only allows a single root node, the stream must be written inside an enclosing node.
It is necessary to call ObjectOutputStream.close() when done, otherwise the stream will be incomplete.
Example
ObjectOutputStream out = xstream.createObjectOutputStream(aWriter, "things"); out.writeInt(123); out.writeObject("Hello"); out.writeObject(someObject) out.close();
- Parameters:
writer
- The writer to serialize the objects to.rootNodeName
- The name of the root node enclosing the stream of objects.- Throws:
IOException
- Since:
- 1.0.3
- See Also:
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
-
createObjectInputStream
public ObjectInputStream createObjectInputStream(Reader xmlReader) throws IOException
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.- Throws:
IOException
- Since:
- 1.0.3
- See Also:
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
,createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
-
createObjectInputStream
public ObjectInputStream createObjectInputStream(InputStream in) throws IOException
Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using XStream.- Throws:
IOException
- Since:
- 1.3
- See Also:
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
,createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
-
createObjectInputStream
public ObjectInputStream createObjectInputStream(HierarchicalStreamReader reader) throws IOException
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.Example
ObjectInputStream in = xstream.createObjectOutputStream(aReader); int a = out.readInt(); Object b = out.readObject(); Object c = out.readObject();
- Throws:
IOException
- Since:
- 1.0.3
- See Also:
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
-
setClassLoader
public void setClassLoader(ClassLoader classLoader)
Change the ClassLoader XStream uses to load classes. Creating an XStream instance it will register for all kind of classes and types of the current JDK, but not for any 3rd party type. To ensure that all other types are loaded with your class loader, you should call this method as early as possible - or consider to provide the class loader directly in the constructor.- Since:
- 1.1.1
-
getClassLoader
public ClassLoader getClassLoader()
Retrieve the ClassLoader XStream uses to load classes.- Since:
- 1.1.1
-
getClassLoaderReference
public ClassLoaderReference getClassLoaderReference()
Retrieve the reference to this instance' ClassLoader. Use this reference for other XStream components (like converters) to ensure that they will use a changed ClassLoader instance automatically.- Returns:
- the reference
- Since:
- 1.4.5
-
omitField
public void omitField(Class definedIn, String fieldName)
Prevents a field from being serialized. To omit a field you must always provide the declaring type and not necessarily the type that is converted.- Throws:
XStream.InitializationException
- if noFieldAliasingMapper
is available- Since:
- 1.1.3
-
ignoreUnknownElements
public void ignoreUnknownElements()
Ignore all unknown elements.- Since:
- 1.4.5
-
ignoreUnknownElements
public void ignoreUnknownElements(String pattern)
Add pattern for unknown element names to ignore.- Parameters:
pattern
- the name pattern as regular expression- Since:
- 1.4.5
-
ignoreUnknownElements
public void ignoreUnknownElements(Pattern pattern)
Add pattern for unknown element names to ignore.- Parameters:
pattern
- the name pattern as regular expression- Since:
- 1.4.5
-
processAnnotations
public void processAnnotations(Class[] types)
Process the annotations of the given types and configure the XStream.- Parameters:
types
- the types with XStream annotations- Since:
- 1.3
-
processAnnotations
public void processAnnotations(Class type)
Process the annotations of the given type and configure the XStream. A call of this method will automatically turn the auto-detection mode for annotations off.- Parameters:
type
- the type with XStream annotations- Since:
- 1.3
-
autodetectAnnotations
public void autodetectAnnotations(boolean mode)
Set the auto-detection mode of the AnnotationMapper. Note that auto-detection implies that the XStream is configured while it is processing the XML steams. This is a potential concurrency problem. Also is it technically not possible to detect all class aliases at deserialization. You have been warned!- Parameters:
mode
-true
if annotations are auto-detected- Since:
- 1.3
-
addPermission
public void addPermission(TypePermission permission)
Add a new security permission.Permissions are evaluated in the added sequence. An instance of
NoTypePermission
orAnyTypePermission
will implicitly wipe any existing permission.- Parameters:
permission
- the permission to add- Since:
- 1.4.7
-
allowTypes
public void allowTypes(String[] names)
Add security permission for explicit types by name.- Parameters:
names
- the type names to allow- Since:
- 1.4.7
-
allowTypes
public void allowTypes(Class[] types)
Add security permission for explicit types.- Parameters:
types
- the types to allow- Since:
- 1.4.7
-
allowTypeHierarchy
public void allowTypeHierarchy(Class type)
Add security permission for a type hierarchy.- Parameters:
type
- the base type to allow- Since:
- 1.4.7
-
allowTypesByRegExp
public void allowTypesByRegExp(String[] regexps)
Add security permission for types matching one of the specified regular expressions.- Parameters:
regexps
- the regular expressions to allow type names- Since:
- 1.4.7
-
allowTypesByRegExp
public void allowTypesByRegExp(Pattern[] regexps)
Add security permission for types matching one of the specified regular expressions.- Parameters:
regexps
- the regular expressions to allow type names- Since:
- 1.4.7
-
allowTypesByWildcard
public void allowTypesByWildcard(String[] patterns)
Add security permission for types matching one of the specified wildcard patterns.Supported are patterns with path expressions using dot as separator:
- ?: one non-control character except separator, e.g. for 'java.net.Inet?Address'
- *: arbitrary number of non-control characters except separator, e.g. for types in a package like 'java.lang.*'
- **: arbitrary number of non-control characters including separator, e.g. for types in a package and subpackages like 'java.lang.**'
- Parameters:
patterns
- the patterns to allow type names- Since:
- 1.4.7
-
denyPermission
public void denyPermission(TypePermission permission)
Add security permission denying another one.- Parameters:
permission
- the permission to deny- Since:
- 1.4.7
-
denyTypes
public void denyTypes(String[] names)
Add security permission forbidding explicit types by name.- Parameters:
names
- the type names to forbid- Since:
- 1.4.7
-
denyTypes
public void denyTypes(Class[] types)
Add security permission forbidding explicit types.- Parameters:
types
- the types to forbid- Since:
- 1.4.7
-
denyTypeHierarchy
public void denyTypeHierarchy(Class type)
Add security permission forbidding a type hierarchy.- Parameters:
type
- the base type to forbid- Since:
- 1.4.7
-
denyTypesByRegExp
public void denyTypesByRegExp(String[] regexps)
Add security permission forbidding types matching one of the specified regular expressions.- Parameters:
regexps
- the regular expressions to forbid type names- Since:
- 1.4.7
-
denyTypesByRegExp
public void denyTypesByRegExp(Pattern[] regexps)
Add security permission forbidding types matching one of the specified regular expressions.- Parameters:
regexps
- the regular expressions to forbid type names- Since:
- 1.4.7
-
denyTypesByWildcard
public void denyTypesByWildcard(String[] patterns)
Add security permission forbidding types matching one of the specified wildcard patterns.Supported are patterns with path expressions using dot as separator:
- ?: one non-control character except separator, e.g. for 'java.net.Inet?Address'
- *: arbitrary number of non-control characters except separator, e.g. for types in a package like 'java.lang.*'
- **: arbitrary number of non-control characters including separator, e.g. for types in a package and subpackages like 'java.lang.**'
- Parameters:
patterns
- the patterns to forbid names- Since:
- 1.4.7
-
-
相关推荐
在压缩包子文件的文件名称列表中,我们只看到"xstream-1.4.2",这可能是XStream-1.4.2版本的源代码或者文档,或者是未打包的JAR文件。解压后,用户可以查看源代码,了解其内部实现,或者进行二次开发。 深入理解...
XStream还支持处理集合和数组,可以将它们转换为XML列表,也可以从XML列表中恢复。此外,对于嵌套的对象和复杂的继承结构,XStream也能处理得游刃有余。 总结起来,XStream作为一个强大的Java库,极大地简化了...
当处理包含集合的类时,XStream会自动将它们转换为XML列表。例如,如果`Person`类包含一个`List<String>`的地址列表,XStream会将每个元素转换为一个单独的XML元素。 ```java @XStreamAlias("person") class Person...
它可以自动处理包含其他对象的复杂对象,以及列表、数组等集合类型。 ### 自定义序列化流程 如果你需要对序列化过程进行更精细的控制,可以实现`HierarchyTraversalStrategy`接口,然后通过`...
XStream的核心是`XStream`类,它提供了`toXML()`方法用于将Java对象转换为XML字符串,以及`fromXML()`方法用于反向操作。要使用XStream,你需要先创建一个`XStream`实例,然后注册你想要序列化的类。 ```java ...
当需要序列化集合时,XStream会生成一个包含元素的XML列表。例如,一个`ArrayList<Person>`会被转换为一个`<list>`标签,其中包含多个`<person>`标签。 7. **错误处理** 在序列化或反序列化过程中可能会遇到错误...
在提供的文件列表中,`Test.java`很可能是测试这些自定义转换器的代码。通常,我们会创建一些测试用例,包括正常情况和边缘情况,来确保我们的转换器工作正常。 ```java public class Test { public static void ...
要创建一个自定义转换器,你需要继承XStream的Converter或HierarchicalStreamConverter接口,并实现其必要的方法。例如,如果你有一个Date类型的字段,想要将其以自定义格式输出,你可以创建一个LocalConverter并...
为了将一个Java对象转换成XML,首先需要创建该对象实例,然后使用XStream的`toXML()`方法: ```java import com.thoughtworks.xstream.XStream; public class Person { private String name; private int age; ...
在给定的文件列表中,有 `xstream-1.3.1.jar` 和 `xpp3_min-1.1.4c.jar` 两个库。`xstream-1.3.1.jar` 是 XStream 库本身,包含了所有必要的类和方法来实现对象与 XML 的转换。而 `xpp3_min-1.1.4c.jar` 是一个解析...
对于数组或列表,可以使用`@XStreamImplicit`注解来避免为每个元素创建单独的XML元素。 在实际项目中,XStream的强大之处在于其灵活性和易用性。它可以轻松地处理复杂的对象结构,并且提供了许多自定义选项,如日期...
在压缩包文件列表中,我们只有一个文件"xstream-1.3.1.jar",这是XStream库的JAR文件,包含所有必要的类和资源,用于在Java项目中导入并使用XStream库。 接下来,我们将深入探讨XStream的核心概念和实际应用: 1. ...
为了序列化Java对象,我们可以创建一个Xstream实例并调用`toXML()`方法: ```java Person person = new Person(); person.setId("1"); person.setFirstName("John"); person.setLastName("Doe"); XStream xstream ...
因此,应当谨慎设置允许序列化的类列表,避免敏感信息泄露。可以通过`xstream.addPermission(NoTypePermission.NONE)`来限制默认行为,并通过`xstream.allowTypesByWildcard(new String[]{"com.example.**"})`指定可...
然而,压缩包子文件的文件名称列表只显示了“新建文件夹”,这意味着具体的示例代码或文件可能在下载后的这个文件夹内,但没有给出详细内容。通常,这样的文件夹可能包含Java源代码文件(.java)、配置文件、测试...
例如,`Person`类包含一个`Address`对象和一个`Contact`列表: ```java public class Address { private String street; private String city; } public class Contact { private String type; private ...
在文件名称列表中,只有一个条目“Xstream”,这可能是博客文章的源代码示例或者一个包含XStream相关示例的压缩包。如果这是一个压缩文件,通常会包含一些Java源代码文件,这些文件展示了如何使用XStream进行序列化...
下面我们将详细探讨XStream的主要功能和使用方法: 1. **对象到XML转换**: 使用XStream,你可以通过简单的API将Java对象转换为XML字符串。例如,你可以创建一个对象实例,然后使用`toXML()`方法将其转换为XML。...
5. 处理集合和数组:XStream可以自动处理Java集合(如List、Set)和数组,将其转换为XML中的元素列表。 在1.4.8版本中,XStream可能包含了一些性能优化、bug修复以及对新Java特性的支持。具体的改进和变更,可以...
对于集合,xStream 会自动将其转换为 XML 的列表元素;对于嵌套对象,它们会被转换为 XML 的子元素。如果需要自定义元素名称或属性,可以通过配置 `XStream` 实例实现,例如: ```java xstream.alias("person", ...