Reputation: 2916
Im trying to use JAXB
to Convert XML to Object, my XML look like this:
<entityResource>
<Item xsi:type="objectPermissionImpl">
<permissionMask>0</permissionMask>
<permissionRecipient xsi:type="roleImpl">
<externallyDefined>false</externallyDefined>
<roleName>ROLE_USER</roleName>
</permissionRecipient>
<URI>repo:/public/adhoc/topics/JSDiagnosticTopic</URI>
</Item>
<Item xsi:type="objectPermissionImpl">
<permissionMask>0</permissionMask>
<permissionRecipient xsi:type="roleImpl">
<externallyDefined>false</externallyDefined>
<roleName>ROLE_ADMINISTRATOR</roleName>
</permissionRecipient>
<URI>repo:/public/adhoc/topics/JSDiagnosticTopic</URI>
</Item>
</entityResource>
So i created 3 java classes : EntityResource.java, Item.java and PermissionRecipient.java as shown bellow:
EntityResource.java
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="entityResource")
public class EntityResource {
List<Item> ls_Item;
public EntityResource() {
}
public List<Item> getLs_Item() {
return ls_Item;
}
@XmlElement(name="Item")
public void setLs_Item(List<Item> ls_Item) {
this.ls_Item = ls_Item;
}
@Override
public String toString() {
return "EntityResource [ls_Item=" + ls_Item + "]";
}
}
Item.java
package model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Item")
public class Item {
int permissionMask;
List<PermissionRecipient> ls_permissionRecipient;
String URI;
public Item() {
}
public int getPermissionMask() {
return permissionMask;
}
@XmlElement(name="permissionMask")
public void setPermissionMask(int permissionMask) {
this.permissionMask = permissionMask;
}
public List<PermissionRecipient> getLs_permissionRecipient() {
return ls_permissionRecipient;
}
@XmlElement(name="permissionRecipient")
public void setLs_permissionRecipient(
List<PermissionRecipient> ls_permissionRecipient) {
this.ls_permissionRecipient = ls_permissionRecipient;
}
public String getURI() {
return URI;
}
@XmlElement(name="URI")
public void setURI(String uRI) {
URI = uRI;
}
@Override
public String toString() {
return "Item [permissionMask=" + permissionMask
+ ", ls_permissionRecipient=" + ls_permissionRecipient
+ ", URI=" + URI + "]";
}
}
PermissionRecipient.java
package model;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="permissionRecipient")
public class PermissionRecipient {
String roleName;
boolean externallyDefined;
public PermissionRecipient() {
}
public boolean isExternallyDefined() {
return externallyDefined;
}
@XmlAttribute(name="externallyDefined")
public void setExternallyDefined(boolean externallyDefined) {
this.externallyDefined = externallyDefined;
}
public String getRoleName() {
return roleName;
}
@XmlAttribute(name="roleName")
public void setRoleName(String rolename) {
this.roleName = rolename;
}
@Override
public String toString() {
return "PermissionRecipient [externallyDefined=" + externallyDefined
+ ", roleName=" + roleName + "]";
}
}
All worked and i got an EntityResource object contain the Item but the permissionRecipient attribute of the Item attribute of EntityResource doesnt contain his attributes (roleName and externallyDefined) !
My unmarshalling code is here:
JAXBContext jaxbContext = JAXBContext
.newInstance(EntityResource.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
EntityResource resourceDescriptors = (EntityResource) jaxbUnmarshaller
.unmarshal(conn.getInputStream());// conn is an HttpURLConnection
The toString()
function of Item returned this results:
[Item
[permissionMask=0,
permissionRecipient=PermissionRecipient [externallyDefined=false, roleName=null],
URI=repo:/public/adhoc/topics/JSDiagnosticTopic],
Item
[permissionMask=0,
permissionRecipient=PermissionRecipient [externallyDefined=false, roleName=null],
URI=repo:/public/adhoc/topics/JSDiagnosticTopic]]
as u can mark, [externallyDefined=false, roleName=null]
in each Item, why ? what mistake i have macked ? Thanks if anyone here can help me solve it, best regards.
Upvotes: 4
Views: 9690
Reputation: 725
In our case we figured a different issue i.e. we had an xml tag like
<tag></tag>
and defined a java class with name Tag. Later when we try to use it in java we declared the variable like below
Tag tag; //didn't worked
and Java class as below
@XmlRootElement(name = "Tag")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Tag", propOrder = {
})
public class Tag{}
and un-marshalling was not working and not loading the values. We changed the variable name from 'tag' to 'Tag' and Bingo! it worked.
Tag Tag; //worked
Another similar situation we had tag name like
<tagNew></tagNew>
Java class as TagNew and variable name was tagNew and it worked fine.
@XmlRootElement(name = "TagNew")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TagNew", propOrder = {
})
public class TagNew{}
TagNew tagNew; // worked fine
May be still something for us to learn! that how JaxB works with naming conventions.
Upvotes: 0
Reputation: 149047
You have roleName
and externallyDefined
mapped with @XmlAttribute
instead of @XmlElement
.
Debugging Tip
When your object model doesn't unmarshal as expected, populate it and marshal it to XML, then compare the output with your input.
Upvotes: 14