users@jaxb.java.net

Re: Help needed mapping a collection of Strings to an element with attribute

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Fri, 28 Nov 2008 10:20:30 +0100

The XML schema for the structure you want is this:

<xs:complexType name="DocType">
  <xs:choice maxOccurs="unbounded">
    <xs:element name="friendList" type="FriendListType"/>
  </xs:choice>
</xs:complexType>

<xs:complexType name="FriendListType">
  <xs:sequence>
    <xs:element name="friend" minOccurs="0" maxOccurs="unbounded">
      <xs:complexType>
        <xs:attribute name="value" type="xs:string"/>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

<xs:element name="doc" type="DocType"/>

The schema compiler xjc creates FriendListType as a class with
an inner class FriendListType.Friend. There is no way around that, because
you have to have some way of keeping both, the tag "friend" *and* the
attribute name "value".

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FriendListType", propOrder = {
    "friend"
})
public class FriendListType {
    protected List<FriendListType.Friend> friend;
    public List<FriendListType.Friend> getFriend() {
        if (friend == null) {
            friend = new ArrayList<FriendListType.Friend>();
        }
        return this.friend;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "")
    public static class Friend {
        @XmlAttribute
        protected String value;
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
}


On Fri, Nov 28, 2008 at 8:59 AM, anbernas <andres.bernasconi_at_gmail.com>wrote:

>
> Sorry if I was too brief, but I don't have a class for FriendList, it's
> just
> a List<String> attribute inside a class. Like:
>
> public class MyClass{
>
> private List<String> friendList = null;
>
> //getters and setters
>
> }
>
> So the <friendList> tag is just a "wrapper" (I can use the
> XmlElementWrapper
> there to make it work), and I get the correct number of elements in my list
> after unmarshalling, but they are all null (I think because it does not
> understand that the String has to come from the value attribute).
>
> I can make it work if the XML is like this:
>
> <friendList>
> <friend>Fred</name>
> <!-- instead of
> <friend value="Fred" />
> -->
> </friendList>
>
> Any Ideas?
>
>
>
> Wolfgang Laun-2 wrote:
> >
> > @XmlType
> > class FriendsList {
> > @XmlElement
> > List<String> friend;
> > }
> >
> > That's all you need.
> > -WL
> >
> > On Thu, Nov 27, 2008 at 11:27 PM, anbernas
> > <andres.bernasconi_at_gmail.com>wrote:
> >
> >>
> >> Hi, I have the following XML (snippet)
> >>
> >> ...
> >> <friendsList>
> >> <friend name="Fred"/>
> >> <friend name="Walter"/>
> >> ...
> >> </friendsList>
> >>
> >> And I want to map that structure to a List<String> (where each element
> in
> >> the list should be "Fred", "Walter", etc.) I would like to try to avoid
> >> creating and intermediate class for Friend that only contains a name. I
> >> am
> >> also using annotations for this, so I would like to stick to that.
> >>
> >> Is there any way to do it?
> >>
> >> I tried the following but I don't understand where I am supposed to
> >> specify
> >> the attribute of the element:
> >> @XmlElementWrapper(name="friendsList")
> >> @XmlElement(name="friend")
> >> //_at_XmlAttribute(name="name") <-- both element and attribute are invalid
> >> protected List<String> friendList;
> >>
> >> Thank you very much for your time!
> >> Regards
> >> AB.
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Help-needed-mapping-a-collection-of-Strings-to-an-element-with-attribute-tp20726382p20726382.html
> >> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> >> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Help-needed-mapping-a-collection-of-Strings-to-an-element-with-attribute-tp20726382p20730247.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>