Showing posts with label xstream. Show all posts
Showing posts with label xstream. Show all posts

XStream Notes


  • If the java class the XStream using is an inner class, you will find something like Outer-class in the xml,  better don't use inner class for XStream.
  • If you have a filed called "children" which is a list of object, but you don't want to show the name of "children", you want only show the list of object directly, you could doxs.addImplicitCollection(Node.class, "children"); in this way, "children" will not show up in xml file.
  • If you want one attribute name showed in side the tag, using xs.useAttributeFor(Employee.class, "name");
  • <employee>
  •     <name>Jack</name>
  •     <role>Manager</role>
  •     <department>Finance</department>
  •   </employee>
  • to 
  • <employee name="Jack">
  •     <role>Manager</role>
  •     <department>Finance</department>
  •   </employee>

XStream Notes

Using xstream-1.4.2.jar, and got the following problem:

Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:336)
at com.hempire.xstream.Writer.main(Writer.java:22)

The XML Pull Parser API defines an own mechanism to load the factory for the available XPP (XML Pull Parser) implementation. XStream's XppDriver never used this lookup mechanism automatically before version 1.4, now it will. Therefore you will have to add a dependency to xmlpull if the XPP implementation does not deliver the classes on its own. This dependency is necessary for Xpp3 in contrast to kXML2 that contains the classes. Use the Xpp3Driver or the KXml2Driver if you want to select one of the directly supported XPP implementation on your own without using the XPP factory. Note, that the minimal version of kXML2 does not support the XPP factory, but can be used by the KXml2Driver.

I honestly don't understand what the above means, but the bottom line is if we include any of the following jars, you should be fine:

kxml2-2.3.0.jar
kxml2-min-2.3.0.jar
xmlpull-1.1.3.1.jar
xpp3_min-1.1.4c.jar

kXML2 is recommend for use as it will greatly improve the performance of XStream.

Supported XML parsers and packages:

XStream

http://xstream.codehaus.org/
http://www.ibm.com/developerworks/xml/library/x-xstream/
Library of serializing object to XML and deserializing object back from XML
1) Example of converting a list of object into a XML file:
public boolean XStream2XML(List importEntryList, String strOutXMLFilePath) 
{
  XStream xs = new XStream();

  //by default, xstream will use class's full name
  //you could use alias to avoid that
  xs.alias("entryList", List.class);
  xs.alias("importentry", ImportEntry.class);
  xs.alias("attribute", Attribute.class);

  try 
  {
    FileOutputStream fs = new FileOutputStream(strOutXMLFilePath);
    xs.toXML(importEntryList, fs);
    //a simpler version
    //String xml = xs.toXML(importEntryList);
    fs.close();
  } 
  catch (Exception e1) 
  {
    //e1.printStackTrace();
    return false;
  }

  return true;
}
2) Example of converting XML back to Java Object
String xml = "......";
Person newJoe = (Person)xstream.fromXML(xml);