Suitable XML Marshaller and Unmarshaller

We have XML that needs to be converted to an object and vice versa. Something like Xstream. So far, we have used Xstream for the marshall and unmarshall / xml object. However, the problem is that an object corresponding to XML in xstream must have all tags as attributes; else if XML contains any additional tags that are not in the object; he bombs.

Or we need custom converters to be written to make sure the operation is as desired. It was also suggested to me that a normal generator would allow Xpath to parse XML from an object.

I am wondering what is the best approach; until:

  • I just want to convert XML to Object and vice versa.
  • Be able to silently ignore any fields in XML that do not appear in the mapping object.

What do you suggest?

+2


source to share


3 answers


You can take a look at this question ...

What is the best way to convert java object to xml using open source apis



Here are some of the libraries he lists ...

+2


source


You need to use a custom MapperWrapper as described here http://pvoss.wordpress.com/2009/01/08/xstream/

XStream xstream = new XStream() {
  @Override
  protected MapperWrapper wrapMapper(MapperWrapper next) {
    return new MapperWrapper(next) {
      @Override
      public boolean shouldSerializeMember(Class definedIn,
              String fieldName) {
        if (definedIn == Object.class) {
          return false;
        }
        return super.shouldSerializeMember(definedIn, fieldName);
      }
    };
  }
};

      



The only thing it does is tell XStream to ignore all fields it doesn't know with.

+2


source


I would suggest using http://simple.sourceforge.net/ I use annotations to map attributes and elements and has a "lax" mode that allows you to read from an XML document while ignoring all attributes and elements not present in the Java object.

0


source







All Articles