java – JAXB unmarshalling无法正常工作
作者:互联网
我试图使用JAXB将XML转换为Object,我的XML看起来像这样:
<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>
所以我创建了3个java类:EntityResource.java,Item.java和PermissionRecipient.java,如下所示:
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 + "]";
}
}
所有工作,我得到一个EntityResource对象包含Item但EntityResource的Item属性的permissionRecipient属性不包含他的属性(roleName和externalDefined)!
我的解组代码在这里:
JAXBContext jaxbContext = JAXBContext
.newInstance(EntityResource.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
EntityResource resourceDescriptors = (EntityResource) jaxbUnmarshaller
.unmarshal(conn.getInputStream());// conn is an HttpURLConnection
Item的toString()函数返回了以下结果:
[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]]
你可以在每个Item中标记[externalDefined = false,roleName = null],为什么?我有什么错误?谢谢,如果有人在这里可以帮助我解决它,最好的问候.
解决方法:
您使用@XmlAttribute而不是@XmlElement映射了roleName和externalDefined.
调试提示
当您的对象模型没有按预期解组时,填充它并将其编组为XML,然后将输出与输入进行比较.
标签:java,xml,jaxb,unmarshalling 来源: https://codeday.me/bug/20190528/1171862.html