semTex
semTex

Reputation: 343

JAXB: Is it possible to handle unknown elements

i get xml content like this:

<data>
   <randomtagname>
      <name>....</name>
      <description>.....</description>
      <description>.....</description>
   </randomtagname>
   <anotherrandomtagname>
      <name>....</name>
      <description>.....</description>
   </anotherrandomtagname>
   <thirdridiculousrandomtagname>
      <name>....</name>
      <description>.....</description>
      <description>.....</description>
   </thirdridiculousrandomtagname>
       .
       .
       .
</data>

is it possibe to create an annotated java class and use jaxb to un-/marshall this file without using @XmlAnyElement??

Upvotes: 2

Views: 1157

Answers (1)

Logan
Logan

Reputation: 2374

It looks like a list of Objects to me. Make a java class that has a

 List<Object> data
variable. You should not need the annotation for that.

Basically you want your xml interface to be generic enough to accept a list of different objects which may have different fields. Once this interface accepts a list of java objects, its up to your java code to determine what they are and cast them to correct class type. Use instanceOf for that part. Some of this is described in java generics where you can pass in any Object, but you have to determine what it is to cast it. I think as long as its sent over in its real class form, instanceof will work. You wouldnt send over a list of Objects, you would send a list of things like String, Integer, or custom classes like Myclass1, Myclass2. Then once you read that list use instanceof to determine the object type of each object in the list, is it instance of String, Myclass1, Myclass2, etc... And cast appropriately.

Reading your question closer I may be missing what you really need to know. Maybe this link hasbsomething that would help. jaxb help

Upvotes: 1

Related Questions