2

My XML file has root element and then many item elements as root's children. So it looks like this:

<root>
   <item type=type1 id=1>...</item>  
   <item type=type1 id=2>...</item>  
   <item type=type2 id=3>...</item>  
   <item type=type3 id=4>...</item>  
</root>

Every item element has attribute that says what type of item we are dealing with. In current XSD xs:element name="root" is as complexType which has sequnce of item complexType. I'm using JAXB to map my XML file to Java objects. Now I have to get all items and according to their types, create new, specific object. It get more and more complicated, as each item type has its own set of fields (child nodes). Is there any chance I can tell JAXB (by XSD) that there are different types of items and according to item's name attribute create object which I need? So for each item there should be separate complexType (which would be mapped to java object).

It would be all ok if my XML looked like this:

<root>
   <item1 id=1>...</item1>
   <item1 id=2>...</item1>
   <item2 id=3>...</item2>
   <item3 id=4>...</item3>
</root>
pavel
  • 525
  • 2
  • 7
  • 14

1 Answers1

0

If you can change the XML so that instead of the unqualified type attribute you can have a qualified type attribute xsi:type (xsi is the prefix associated with the XML Schema Instance namespace) then:

  • your schema needs to provide definitions for those types

That would be it...

If you can't (worth a try) maybe this post on SO could help you.

Community
  • 1
  • 1
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62