0

I have an object similar to the following:

public class FooObj
{
    private Long id;
    private List<BarObj> subBar;
    private String someStr;

    public Long getId()
    {
        return id;
    }

    public List<BarObj> getSubBar()
    {
        return subBar;
    }

    public String getSomeStr()
    {
        return someStr;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public void setSubBar(List<BarObj> subBar)
    {
        this.subBar = subBar;
    }

    public void setSomeStr(String someStr)
    {
        this.someStr = someStr;
    }

    public static class BarObj
    {
        private String groupId;
        private Long id;

        public String getGroupId()
        {
            return groupId;
        }

        public Long getId()
        {
            return id;
        }

        public void setGroupId(String groupId)
        {
            this.groupId = groupId;
        }

        public void setId(Long id)
        {
            this.id = id;
        }
    }
}

And I want to map the Object to an XML structure like the XML in the link: http://pastebin.com/cw018jqc EDIT:(Please look at the ObjBars element for an exact definition of what I'm looking for.)

Is there any library available that would allow me to do this?

  • You can use JAXB, but your classes need some annotations. Take a look at http://stackoverflow.com/questions/7212064/is-it-possible-to-generate-a-xsd-from-a-jaxb-annotated-class – nakosspy Jun 03 '13 at 17:17
  • That's not really what I'm looking for. I'm not trying to generate an XSD, I have existing classes that I want to map to an XML Structure that doesn't match up exactly to the Object. IE Predefined XML to Predefined object. –  Jun 03 '13 at 17:27
  • If you want to match pre-existing classes to pre-existing XSD (XML structure) and these two won't match, then I don't think that you will be able to do automatically. Maybe first convert your objects to something that matches exactly the XML structure and then serialize to XML with JAXB. – nakosspy Jun 03 '13 at 17:39
  • I'm not really looking for automatically, just not so manual. IE some sort of annotations or pathing that I can use to preconfigure it. –  Jun 03 '13 at 17:45

1 Answers1

1

So you're basically trying to split a list into multiple sublists before you serialize it to XML? I think that JAXB could really help you here. I think you could use an @XmlTypeAdapter to convert between List<BarObj> and List<List<BarObj>>, which would be one way of representing this data the way you want it marshalled to XML. Check out http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html for details.

Stephen Carlson
  • 276
  • 1
  • 9
  • Thank you. I really wish I had seen this earlier! –  Jun 04 '13 at 06:07
  • I'm now less optimistic than when I posted this. A few attempts at trying to annotate a List property with XmlJavaTypeAdapter has demonstrated to me that JAXB passes the list element type, and not the List itself, to marshal(). So what you'd end up having to do here is use XmlJavaTypeAdpater at the class level to convert between your class and one that has a structure that maps 1:1 with the XML. Essentially what nakosspy said above. – Stephen Carlson Jun 04 '13 at 06:24
  • Is the reason you can't change the target object because you don't have control over the code, or because you're trying to preserve the interface? Because I think you could introduce a new internal structure that maps 1:1 to your XML without changing the external interface at all. – Stephen Carlson Jun 04 '13 at 06:27
  • That's what I've ended up doing. One is a generated class, the other is a preexisting XML definition that I have to follow, but it's not a good definition in my opinion, so I defined the new objects. –  Jun 04 '13 at 06:52