Reputation: 348
I am trying to unmarshal xml response(parsing with JAXB) conforming to following xsd:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="response" type="responseType" />
<xsd:complexType name="responseType">
<xsd:sequence>
<xsd:element name="result" type="resultType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="resultType">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="doc" type="docType" />
</xsd:sequence>
<xsd:attribute name="start" type="xsd:int" />
</xsd:complexType>
<xsd:complexType name="docType">
<xsd:sequence>
<xsd:element minOccurs="1" name="int" type="intType" maxOccurs="unbounded" />
<xsd:element maxOccurs="unbounded" minOccurs="1" name="str" type="strType" />
<xsd:element name="float" type="floatType" />
<xsd:element maxOccurs="unbounded" minOccurs="1" name="long" type="longType" />
<xsd:element name="date" type="dateType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="dateType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="longType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="floatType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="strType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="intType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
I've also generated the classes successfully using xjc tool. But, I am unable to get the text of all maxoccurs unbounded elements : int,str,long nodes. Since, these elements are unbounded, I get a list of each of these element of type ArrayList through methods in generated classes. And, a method to get value of the attribute. But generated classes don't provide a method to get value of the text node.
For eg: if my response is:
<response>
<result start="0">
<doc>
<int name="age">0</int>
<str name="continent">EU</str>
<str name="country">United Kingdom</str>
<float name="influence">7.0</float>
<int name="classified">0</int>
<date name="createdOnGMTDate">2011-10-12T08:11:07Z</date>
<int name="favCnt">41</int>
<long name="id">120493600</long>
<long name="inReplyToId">-1</long>
<str name="isBookmarked">False</str>
<str name="lang">english</str>
<str name="language">english.lm</str>
<int name="listedCnt">6</int>
<str name="name">john doe</str>
<str name="screenName">john_d</str>
<int name="sentiment">-1</int>
<str name="timeZone">London</str>
<str name="Text">Yeah gee got flashed in the W.C.....</str>
<str name="usrCrtDate">2011-10-12 08:11:07</str>
</doc>
<result>
<response>
then, I able to retrieve attribute names like continent, country but not its node values EU, United Kingdom respectively.
I think there's some issue in my xsd or in generated classes. Kindly assist.
Thanks.
Upvotes: 1
Views: 1382
Reputation: 149057
Your XML schema should look something like the following to capture both an attribute and text value:
<xsd:complexType name="floatType">
<xsd:simpleContent>
<xsd:extension base="xsd:float">
<xsd:attribute name="name" type="xsd:string" />
</xsd:extension
</xsd:simpleContent>
</xsd:complexType>
The text node will be mapped in JAXB with @XmlValue
. For more information see:
Upvotes: 1
Reputation: 32437
I don't think your xml conforms to the schema you provided.
<xsd:complexType name="strType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
That doesn't make any mention of a child element. I think you need to extend the xsd base types (xsd:string etc.) and add an attribute.
Upvotes: 0