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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With