简体   繁体   中英

Map one object structure to an different XML structure

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM