Reputation: 170
I have an xsd schema
<xs:complexType name="valueType" mixed="true">
<xs:sequence>
<xs:element name="filename" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
and XML
<datapoint>
<fieldname>somestring</fieldname>
<value>some string</value>
</datapoint>
<datapoint>
<fieldname>somestring</fieldname>
<value>some string</value>
</datapoint>
<datapoint>
<fieldname>somestring</fieldname>
<value>
</filedata>
<filename>some string</filename>
</value>
</datapoint>
i used JAXB parser and genarated a class
public static class Value {
@XmlElementRef(name = "string", type = JAXBElement.class)
@XmlMixed
protected List<Serializable> content;
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
}
i am able to get the values using ValueObj.getContent() when
<datapoint>
<fieldname>somestring</fieldname>
<value>some string</value>
</datapoint>
but i am getting an object for filename. how to read string from List when
<datapoint>
<fieldname>somestring</fieldname>
<value>
<filename>some string</filename>
</value>
</datapoint>
do i have to use two xsd or one xsd? how can i get the values inside tag as well as inside tag.
Upvotes: 0
Views: 700
Reputation: 2375
The List<Serializable>
will contain String
s for plain text parts and JAXBElement<?>
objects for XML elements. You can use JAXBElement.getName()
to check for the element's QName. In case you have complex types, you can get the corresponding java type (which is erased from JAXBElement due to Java type erasure) using JAXBElement.getDeclaredType()
.
Upvotes: 1