JAXB Unmarshalling @XmlAnyElement

15,943

You need to add @XmlRootElement on the classes you want to appear as instances in the field/property you have annotated with @XmlAnyElement(lax=true).

Java Model

Home

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Home {
    @XmlAnyElement(lax = true)
    protected List<Object> any;

    //setter getter also implemented
}

Person

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Person")
public class Person {

}

Animal

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Animal")
public class Animal {

}

Demo Code

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <Person/>
    <Animal/>
    <Person/>
</root>

Demo

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Home.class, Person.class, Animal.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource xml = new StreamSource("src/forum20329510/input.xml");
        Home home = unmarshaller.unmarshal(xml, Home.class).getValue();

        for(Object object : home.any) {
            System.out.println(object.getClass());
        }
    }

}

Output

class forum20329510.Person
class forum20329510.Animal
class forum20329510.Person

For More Information

Share:
15,943

Related videos on Youtube

hiddenuser
Author by

hiddenuser

Updated on October 03, 2022

Comments

  • hiddenuser
    hiddenuser over 1 year

    I have created three JAXB class : Home , Person , Animal . Java Class Home have variable List<Object> any that may contain Person and/or Animal instance .

        public class Home {
            @XmlAnyElement(lax = true)
            protected List<Object> any;
        //setter getter also implemented
        }
    @XmlRootElement(name = "Person")                            // Edited
        public class Person {
            protected String name; //setter getter also implemented
         } 
    @XmlRootElement(name = "Animal")                             // Edited
        public class Animal {
           protected String name; //setter getter also implemented
         }
    

    /* After Unmarshalling */

     Home home ;
    
                    for(Object obj : home .getAny()){
                        if(obj instanceof Person ){
                            Person  person = (Person )obj;
                            // .........
                        }else if(obj instanceof Animal ){
                            Animal animal = (Animal )obj;
                            // .........
                        }
                    }
    

    I need to achieve Person or Animal object saved in "Home.any" List variable but content of "Home.any" List is instance of com.sun.org.apache.xerces.internal.dom.ElementNSImpl instead of Animal or Person .

    So is there a way to achieve Animal or Person instance that is saved in xml in "Home.any" List.

  • hiddenuser
    hiddenuser over 10 years
    I have already added @XmlRootElement(name = "Person") and @XmlRootElement(name = "Animal") also added entry in ObjectFactory.java .
  • bdoughan
    bdoughan over 10 years
    @userG - How are you creating your JAXBContext and is it aware of all your classes?
  • hiddenuser
    hiddenuser over 10 years
    According to your post , i think ; there is a problem with JAXBContext Declaration JAXBContext.newInstance(Home.class) .
  • hiddenuser
    hiddenuser over 10 years
    I have already defined both @XmlRootElement and entry in ObjectFactory.java . Is i have to define only one of them ?
  • hiddenuser
    hiddenuser over 10 years
    JAXBContext.newInstance(Home.class,Person.class,Animal.class‌​) , @XmlRootElement already added and entry from ObjectFactory.java commented but still not resolved .
  • bdoughan
    bdoughan over 10 years
    @userG - I have updated my answer with a complete code example.
  • hiddenuser
    hiddenuser over 10 years
    Thanks @Blaise for your guidance , tested your code working fine as stated . I am working on netbeans plugin and using api org.netbeans.modules.xml.jaxb.api , i am still facing this problem and for temporary solution , passing dom.Element object to unmarshall method again to retrieve jaxb object . (I have also seen JAXBContext.typeMap Person & Animal both reference exist .)
  • Marco
    Marco over 9 years
    What about java.lang.* classes like String? Any tips on how to unmarshall a List<String>?