This is the generated code for "PersonType" class.
package demo;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonType", propOrder = {
"name",
"address"
})
public class PersonType {
@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Address", required = true)
protected List<AddressType> address;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public List<AddressType> getAddress() {
if (address == null) {
address = new ArrayList<AddressType>();
}
return this.address;
}
}
This is the generated code for "AddressType" class.
package demo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AddressType", propOrder = {
"number",
"street"
})
public class AddressType {
@XmlElement(name = "Number")
@XmlSchemaType(name = "unsignedInt")
protected long number;
@XmlElement(name = "Street", required = true)
protected String street;
public long getNumber() {
return number;
}
public void setNumber(long value) {
this.number = value;
}
public String getStreet() {
return street;
}
public void setStreet(String value) {
this.street = value;
}
}
This is the xml file I want to unmarshal
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\JAXB Demo\demo.xsd">
<Name>Sharon Krisher</Name>
<Address>
<Street>Iben Gevirol</Street>
<Number>57</Number>
</Address>
<Address>
<Street>Moshe Sharet</Street>
<Number>89</Number>
</Address>
</Person>
Code foe unarchall
JAXBContext context = JAXBContext.newInstance(PersonType.class);
Unmarshaller unmarshaller =context.createUnmarshaller();
//unmarshaller.setValidating(true);
PersonType person =(PersonType) unmarshaller.unmarshal(new File("C:\\Users\\sithi\\workspace\\TestJAXB\\src\\data.xml") );
This code gives an exception:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"PersonType"). Expected elements are (none)
There are two ways to associate a class with a root element. The first is @XmlRootElement
on the class, the second is an @XmlElementDecl
annotation on a class annotated with @XmlRegistry
(when the model is generated from an XML a Schema this class is called ObjectFactory
.
Creating the JAXBContext
When you generate a model from XML Schema you should create the JAXBContext
on the package name:
JAXBContext jc = JAXBContext.newInstance("demo");
Or generated ObjectFactory
class:
JAXBContext jc = JAXBContext.newInstance(demo.ObjectFactory.class);
This will make sure the ObjectFactory
class is processed.
No @XmlRootElement
or @XmlElementDecl
?
If there isn't an @XmlRootElement
or @XmlElementDecl
associating a class with the root element of your document then you will need to use one of the unmarshal methods that take a class parameter.
PersonType pt = unmarshaller.unmarshal(xml, PersonType.class).getValue();
The issue is that your PersonType class doesn't have @XmlRootElement
. I think you'd want to do the "Unmarshalling into a known type" example in this page, which doesn't require @XmlRootElement on PersonType .
来源:https://stackoverflow.com/questions/19899882/unmarshal-error-in-jaxb