0

I want to create an JAVA object, that could be converted into XML structure

<a id="[numeric value]">[string value]</a>

I read the tutorials on Xstream site and googled some time, but I can't find solutions.

For example, to convert the POJO to

<many_a>
    <a id="[numeric value]">
         <name>[string value]</name>
    </a>
    ...
</many_a>

I will create POJOs

@XStreamAlias("many_a")
class AList{
    @XStreamImplicit(itemFieldName = "a")
    List<A> list;
}
class A{
    @XStreamAsAttribute
    @XStreamAlias("id")
    long id;
    @XStreamAlias("name")
    String name;
}

So, the question is which POJO I should create to get the XML above?

xrabbit
  • 725
  • 1
  • 7
  • 22
  • you can use Map to marshal your object – user3487063 Aug 21 '14 at 18:54
  • so, the only solution is a custom converter? – xrabbit Aug 21 '14 at 19:04
  • Have a look at http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/converters/collections/MapConverter.html too – user3487063 Aug 21 '14 at 19:12
  • I feel like I dont quite understand the question, but this is the easiest API ive used for serializing to XML with a POJO type object, if using something other than XStream is an option: http://simple.sourceforge.net/ – Mark W Aug 21 '14 at 19:59

2 Answers2

0

You can use Map<Integer,String> to marshal your object , if you have all entries in your xml as you specified. Please refer to this question to get an idea.

Community
  • 1
  • 1
user3487063
  • 3,672
  • 1
  • 17
  • 24
0

I found the solution how to solve this issue without writing your custom converter. See this thread:

XStream: convert collection with attributes

Community
  • 1
  • 1
xrabbit
  • 725
  • 1
  • 7
  • 22