This question already has an answer here:
Issue with namespaces in XMLs in JAXB unmarshalling
(1 answer)
Closed 8 years ago.
I have a simple xml to unmarshall. But I get only an empty list in the output. No exceptions are thrown.
This is a third party generated xml and I need to make this work without any changes in the xml.
The XML:
<Animal xmlns="http://allmycats.com/serviceplatform/1.0/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Cat z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty</name>
</Cat>
<Cat z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty2</name>
</Cat>
</Animal>
The POJO bean for Animal:
#XmlRootElement(name = "Animal",namespace = "http://allmycats.com/serviceplatform/1.0/")
public class Animal{
List<Cat> cats;
#XmlElement(name = "Cat")
public List<Cat> getCats() {
return cats;
}
public void setCats(List<Cat>cats) {
this.cats= cats;
}
}
The POJO bean for Cat
#XmlRootElement(name = "Cat")
public class Cat {
private String zId;
private String name;
#XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
public String getzId() {
return zId;
}
public void setzId(String zId) {
this.zId = zId;
}
#XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The unmarshall code is:
File file = new File(filepath);
System.out.println("file exists? : "+ file.exists()); // prints true
JAXBContext jaxbContext = JAXBContext.newInstance(Animal2.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Animal2 animals = (Animal2)jaxbUnmarshaller.unmarshal( file );
System.out.println("--file size: "+animals.getCats().size());
The last line gives a null pointer exception.
Am I doing something wrong with the namespaces in the BEAN classes? I am new to Jaxb and this issue is bugging me for 3 days now !
I asked this question earlier but couldn't get proper answer and this is a more precise question.
Animal.java
import java.io.Serializable;
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.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlRootElement(name = "Animal",namespace = "http://allmycats.com/serviceplatform/1.0/")
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "Animal", propOrder = {
"cats"
})
public class Animal implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#XmlElement(name = "Cat")
protected List<Cat> cats;
public List<Cat> getCats() {
if (cats == null) { // This solve the nullpointer
cats = new ArrayList<Cat>();
}
return this.cats;
}
}
Cat.java
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlRootElement(name = "Cat")
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "Cat", propOrder = {
"name"
})
public class Cat implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
private String zId;
#XmlElement(name = "name")
private String name;
public String getzId() {
return zId;
}
public void setzId(String zId) {
this.zId = zId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Related
Would like to create the following XML element using JAXB, no value(content), no closing element name, just closing '/' :
<ElementName attribute1="A" attribute2="B"" xsi:type="type" xmlns="some_namespace"/>
Trying the following
#XmlAccessorType(XmlAccessType.FIELD)
public class ElementName {
#XmlElement(name = "ElementName", nillable = true)
protected String value;
#XmlAttribute(name = "attribute1")
protected String attribute1;
#XmlAttribute(name = "attribute2")
protected String attribute2;
}
When constructing an object of this type as below, there is an exception
ElementName element = new ElementName();
What is the correct way of doing it ?
In case you want to achieve it for ElementName with value set to null remove nillable attribute. Simple example how to generate XML payload:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class JaxbApp {
public static void main(String[] args) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(ElementName.class);
ElementName en = new ElementName();
en.attribute1 = "A";
en.attribute2 = "B";
en.value = null;
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(en, System.out);
}
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "ElementName")
class ElementName {
#XmlElement(name = "ElementName")
protected String value;
#XmlAttribute(name = "attribute1")
protected String attribute1;
#XmlAttribute(name = "attribute2")
protected String attribute2;
}
prints:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ElementName attribute1="A" attribute2="B"/>
Unbelievable how many questions there are already about this subject. Still, none of them helps me because what I understood is that it is a common error and I believe my code is correct because it is exactly the same as the one of another project my colleague passed me.
But.. I have this error
unable to marshal type "modules.CollaborationInfo" as an element because it is missing an #XmlRootElement annotation
Given this class CollaborationInfo:
package modules;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "CollaborationInfo", propOrder = {
"AgreementRef",
"ConversationId"
})
public class CollaborationInfo {
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name="AgreementRef")
public static class AgreementRef {
#XmlAttribute
private String pmode;
public String getPmode() {
return pmode;
}
public void setPmode(String pmode) {
this.pmode = pmode;
}
}
#XmlElementRef(name = "AgreementRef")
protected AgreementRef AgreementRef = new AgreementRef();
#XmlElement(name="ConversationId")
protected String ConversationId;
public String getPmode() {
return AgreementRef.getPmode();
}
public void setPmode(String value) {
this.AgreementRef.setPmode(value);
}
public String getConversationId() {
return ConversationId;
}
public void setConversationId(String value) {
this.ConversationId = value;
}
}
And as main():
public static void main(String[] args) throws Exception{
JAXBContext contextObj = JAXBContext.newInstance(CollaborationInfo.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
CollaborationInfo CI = new CollaborationInfo();
CI.setPmode("String1");
CI.setConversationId("String2");
marshallerObj.marshal(CI, new FileOutputStream("C:\\OUT.xml"));
}
I should be able to have as output (OUT.xml):
<?xml version="1.0" encoding="UTF-8"?>
<CollaborationInfo>
<AgreementRef pmode="String1"/>
<ConversationId>String2</ConversationId>
</CollaborationInfo>
But I just can't.
Can someone tell me where I am wrong?
(Of course the real XML is much more long and complex, but probably if I'm able to have this part working I'm able to continue the rest)
As the errors says, you need to add the annotation #XmlRootElement in front of the class CollaborationInfo. Your #XmlRootElement is for the static class AgreementRef.
package modules;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "CollaborationInfo", propOrder = {
"AgreementRef",
"ConversationId"
})
public class CollaborationInfo {
#XmlAccessorType(XmlAccessType.FIELD)
public static class AgreementRef {
#XmlAttribute
private String pmode;
public String getPmode() {
return pmode;
}
public void setPmode(String pmode) {
this.pmode = pmode;
}
}
#XmlElement(name = "AgreementRef")
protected AgreementRef AgreementRef = new AgreementRef();
#XmlElement(name="ConversationId")
protected String ConversationId;
public String getPmode() {
return AgreementRef.getPmode();
}
public void setPmode(String value) {
this.AgreementRef.setPmode(value);
}
public String getConversationId() {
return ConversationId;
}
public void setConversationId(String value) {
this.ConversationId = value;
}
}
This is old but just in case someone runs into it, it was answered here:
https://stackoverflow.com/a/59249216/5835746
Basically, you should not use the auto-generated classes directly but use the methods from ObjectFactory, which was also auto-generated by the plugin, to create instances of those classes.
Editing the auto-generated classes, to add the #XmlRootElement annotation is probably not a good idea.
I am getting below exception, i need some help to resolve the issue.
If remove the namespace in the object factory and with out package-info.java class it is working fine.
Exception that is throwing now
Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
There's no ObjectFactory with an #XmlElementDecl for the element {}shipping.
this problem is related to the following location:
at protected javax.xml.bind.JAXBElement com.jverstry.annotations.generics.Market$Detail.shipping
at com.jverstry.annotations.generics.Market$Detail
at protected com.jverstry.annotations.generics.Market$Detail com.jverstry.annotations.generics.Market.detail
at com.jverstry.annotations.generics.Market
ObjectFactory class which is creating the jaxbelement
package com.jverstry.annotations.generics;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
import org.example.customer.Customer;
#XmlRegistry
public class ObjectFactory {
public ObjectFactory() {
}
public Market.Detail.Shipping createShipping() {
return new Market.Detail.Shipping();
}
private final static QName _Shipping_QNAME = new QName("http://www.example.org/customer", "shipping");
#XmlElementDecl(namespace = "http://www.example.org/customer", name = "shipping")
public JAXBElement<Market.Detail.Shipping> createShipping(Market.Detail.Shipping value) {
return new JAXBElement<Market.Detail.Shipping>(_Shipping_QNAME, Market.Detail.Shipping.class, value);
}
}
Class package-info.java, where the name spaces are mentioned for the response xml
#XmlSchema(namespace = "http://www.example.org/customer", elementFormDefault = XmlNsForm.QUALIFIED)
package com.jverstry.annotations.generics;
import javax.xml.bind.annotation.*;
Demo class where marshalling object
package com.jverstry.annotations.generics;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Market.class);
Market market = new Market();
Market.Detail md = new Market.Detail();
Market.Detail.Shipping mds = new Market.Detail.Shipping();
mds.setAvailable(false);
JAXBElement<Market.Detail.Shipping> shipping = new ObjectFactory().createShipping(mds);
shipping.setNil(true);
md.setShipping(shipping);
market.setDetail(md);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(market, System.out);
}
}
Market class, this is the main root class where jaxbcontext is created
package com.jverstry.annotations.generics;
import java.math.BigDecimal;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "detail" })
#XmlRootElement(name = "Market")
public class Market
{
#XmlElement(required = false)
protected Market.Detail detail;
public Market.Detail getDetail() {
return detail;
}
public void setDetail(Market.Detail detail) {
this.detail = detail;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "shipping" })
public static class Detail
{
#XmlElementRef(name = "shipping")
protected JAXBElement<Market.Detail.Shipping> shipping;
public JAXBElement<Market.Detail.Shipping> getShipping() {
return shipping;
}
public void setShipping(JAXBElement<Market.Detail.Shipping> value) {
this.shipping = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "value" })
public static class Shipping
{
#XmlValue
protected BigDecimal value;
#XmlAttribute(name = "available")
protected Boolean available;
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
public Boolean getAvailable() {
return available;
}
public void setAvailable(Boolean value) {
this.available = value;
}
}
}
}
You need to create the JAXBContext by passing in the ObjectFactory class or the package name of the generated model to ensure the ObjectFactory class is processed.
If you specify the namespace property on the #XmlElementRef annotation things should work.
I have class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "customerOrder", propOrder = {
"demandsUuid",
"invoicesOutUuid",
"paymentsUuid",
"customerOrderPosition",
"purchaseOrdersUuid"
})
public class CustomerOrder extends LegendEntity {
#XmlAttribute(name = "sourceAgentUuid")
#XmlIDREF
#XmlSchemaType(name = "IDREF")
#XmlJavaTypeAdapter(XmlAdapterTest.class)
protected Object sourceAgentUuid;
....
Also Agent class for sourceAgentUuid field
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "agent", propOrder = {
"attribute",
"requisite",
"contact",
"contactPerson",
"agentNewsItem",
"tags"
})
#XmlSeeAlso({
Company.class
})
public class Agent
extends LegendEntity
{
#XmlJavaTypeAdapter(CollapsedStringAdapter.class)
#XmlID
#XmlSchemaType(name = "ID")
protected String uuid;
And unmarshalling xml snipetpets
<?xml version="1.0" encoding="UTF-8"?>
<customerOrder deliveryPlannedMoment="2014-04-23T16:10:00+04:00" reservedSum="0.0" stateUuid="44b3fca5-a2b3-11e3-31b2-002590a28eca" targetAgentUuid="9a3c7d6b-4245-11e3-24c6-7054d21a8d1e"
sourceAgentUuid="ef0b03de-c95b-11e3-9183-002590a28eca" sourceStoreUuid="b05ed064-8743-11e3-0c1a-002590a28eca"
applicable="true" moment="2014-04-21T17:50:00+04:00" payerVat="true" rate="1.0" vatIncluded="true" created="2014-04-21T17:50:44.142+04:00" createdBy="it#erms" name="440" updated="2014-04-22T12:19:56.472+04:00" updatedBy="it#erms" readMode="SELF" changeMode="NONE">
<accountUuid>5f65cc9e-3708-11e3-bf9a-7054d21a8d1e</accountUuid>
<accountId>5f65cc9e-3708-11e3-bf9a-7054d21a8d1e</accountId>
<uuid>ef1267b8-c95b-11e3-aeb4-002590a28eca</uuid>
<groupUuid>9a3ad96b-4245-11e3-010e-7054d21a8d1e</groupUuid>
<groupId>9a3ad96b-4245-11e3-010e-7054d21a8d1e</groupId>
<code>440</code>
<externalcode>440</externalcode>
When I try to unmarshall
JAXBElement poe = (JAXBElement)u.unmarshal( new FileInputStream( "org_order.xml" ) );
CustomerOrder co=(CustomerOrder)poe.getValue();
System.out.println(co.getSourceAgentUuid());
result null (but all other not #XmlIDREF field unmarshalled ok)
How I can get ef0b03de-c95b-11e3-9183-002590a28eca result ?
I had create test class for simply.
Why output is null?
import java.io.ByteArrayInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class TestMarshall {
public static void main(String[] args) {
try {
// marshal();
unmarshal();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void marshal() throws Exception {
CustomerOrder co = new CustomerOrder();
Agent a = new Agent();
a.setUuid("sdfsdf");
co.setSourceAgentUuid(a);
JAXBContext jc = JAXBContext.newInstance(CustomerOrder.class,
Agent.class);
Marshaller m = jc.createMarshaller();
m.marshal(co, System.out);
}
public static void unmarshal() throws Exception {
JAXBContext jc = JAXBContext.newInstance(CustomerOrder.class,
Agent.class);
Unmarshaller u = jc.createUnmarshaller();
String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><customerOrder sourceAgentUuid=\"sdfsdf\"/>";
CustomerOrder poe = (CustomerOrder) u
.unmarshal(new ByteArrayInputStream(testXML.getBytes()));
CustomerOrder co = poe;
System.out.println("sourceAgentUuid= " + co.getSourceAgentUuid());
}
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "agent")
class Agent {
#XmlJavaTypeAdapter(CollapsedStringAdapter.class)
#XmlID
#XmlSchemaType(name = "ID")
protected String uuid;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement
#XmlType(name = "customerOrder")
class CustomerOrder {
#XmlAttribute(name = "sourceAgentUuid")
#XmlIDREF
#XmlSchemaType(name = "IDREF")
protected Object sourceAgentUuid;
public Object getSourceAgentUuid() {
return sourceAgentUuid;
}
public void setSourceAgentUuid(Object sourceAgentUuid) {
this.sourceAgentUuid = sourceAgentUuid;
}
}
An #XmlIDREF plus its counterpart #XmlID are "XMLized pointers" that appear as strings in the XML text. JAXB matches such value pairs and resolves matching references into nice object references.
So, after unmarshalling, you won't see the strings from the XML file; the
Object sourceAgentUuid
(in your case) will either contain a reference to that "source agent" object (if there is such an XML element with its #XmlID set to that value) or null otherwise.
I have the below XML that I want to read. Using JAXB on java 1.6, how do I annotate for the attribute regex ? Can I have the field to be of type boolean ?
<?xml version="1.0" encoding="utf-8"?>
<authStore>
<authList>
<auth>
<resource>res1</resource>
<privilege regex = "true">PRIV_FILE_.+?_READ</privilege>
</auth>
<auth>
<resource>res2</resource>
<privilege>PRIV_FILE_READ</privilege>
</auth>
</authStore>
UPDATE : Is it possible to make the attribute optional ? If yes, when I unmarshal, will I get regex field to be false when a privilege element does not have the optional attribute regex ?
UDPATE2 : I don't want to define separate classes for resource and privilege. Also, I don't want to use MOXy. Pls. suggest solution for sun/oracle JDK 1.6 JAXB only.
UPDATE3 : My current object model is something like this
// AuthStore.java
#XmlRootElement
public class AuthStore {
#XmlElementWrapper(name = "authList")
#XmlElement(name = "auth")
private ArrayList<Auth> authList;
public void setAuthList(ArrayList<Auth> authList) {
this.authList = authList;
}
public ArrayList<Auth> getAuthsList() {
return authList;
}
}
// Auth.java
#XmlRootElement(name = "auth")
#XmlType(propOrder = { "resource", "privilege" })
public class Auth
{
private String resource;
private String privilege;
#XmlElement(name = "resource")
public String getResource()
{
return resource;
}
public void setResource(String resource)
{
this.resource = resource;
}
#XmlElement(name = "privilege")
public String getPrivilege()
{
return privilege;
}
public void setPrivilege(String author)
{
this.privilege = author;
}
}
Because privilege contains an attribute (It's actually complex type), you must create a class to hold both the value and the attribute:
import java.io.InputStream;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
#XmlRootElement(name = "authStore")
#XmlAccessorType(XmlAccesssType.FIELD)
public class AuthStore {
public static void main(String []args) throws Exception {
InputStream inputStream = AuthStore.class.getResourceAsStream("test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(AuthStore.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
AuthStore authStore = (AuthStore)unmarshaller.unmarshal(inputStream);
System.out.println(authStore.getAuthList().get(0).getResource());
System.out.println(authStore.getAuthList().get(0).getPrivilege().getRegex());
System.out.println(authStore.getAuthList().get(0).getPrivilege().getValue());
}
#XmlElementWrapper(name = "authList")
#XmlElement(name = "auth")
private List<Auth> authList;
public List<Auth> getAuthList() {
return authList;
}
#XmlAccessorType(XmlAccesssType.FIELD)
public static class Auth {
#XmlElement(name = "resource")
private String resource;
#XmlElement(name = "privilege")
private Privilege privilege;
public String getResource() {
return resource;
}
public Privilege getPrivilege() {
return privilege;
}
#XmlAccessorType(XmlAccesssType.FIELD)
public static class Privilege {
#XmlAttribute(name = "regex")
private Boolean regex;
#XmlValue
private String value;
public Boolean getRegex() {
return regex;
}
public String getValue() {
return value;
}
}
}
}