Question

I have a xml likethis

<datapoint>
   <fieldname>somestring</fieldname>
   <value>some string</value>
</datapoint>
<datapoint>
   <fieldname>somestring</fieldname>
   <value>some string</value>
</datapoint>
<datapoint>
   <fieldname>somestring</fieldname>
   <value>
       <filename>some string</filename>
   </value>
</datapoint>

I need to define XSD for this XML. I used value as complex type . but when i use value as complex type i am not able to parse the string value from Value in JAXB. I am getting a object only. If i declare value as simple type(String) i am not able to read the filename.what should i do..please help.

Was it helpful?

Solution

You mark an XML element as optional by including minOccurs="0" in the definition.


I used value as complex type . but when i use value as complex type i am not able to parse the string value from Value in JAXB

If you just define the complex type then you will need to pass the corresponding class to the unmarshal method:

Datapoint datapoint = unmarshaller.unmarshal(xml, Datapoint.class).getValue();

You do not need to use the class parameter if you define a global element for the complex type. If you define a global element that references a named complex type you will get an instance of JAXBElement<Datapoint> back, and if you define a global element with an anonymous type you will get back an instance of Datapoint.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top