[英]JAXB unmarshalling not working properly
Im trying to use JAXB
to Convert XML to Object , my XML look like this:我正在尝试使用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>
So i created 3 java classes : EntityResource.java, Item.java and PermissionRecipient.java as shown bellow:所以我创建了 3 个 java 类:EntityResource.java、Item.java 和 PermissionRecipient.java,如下所示:
EntityResource.java实体资源.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项目.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权限接收者.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) !一切正常,我得到了一个包含 Item 的 EntityResource 对象,但 EntityResource 的 Item 属性的 permissionRecipient 属性不包含他的属性(roleName 和 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的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]]
as u can mark, [externallyDefined=false, roleName=null]
in each Item, why ?正如您可以在每个项目中标记[externallyDefined=false, roleName=null]
,为什么? 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
.您将roleName
和externallyDefined
映射到@XmlAttribute
而不是@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.当您的对象模型没有按预期解组时,填充它并将其编组为 XML,然后将输出与您的输入进行比较。
In our case we figured a different issue ie we had an xml tag like在我们的例子中,我们发现了一个不同的问题,即我们有一个 xml 标签,如
<tag></tag>
and defined a java class with name Tag.并定义了一个名为 Tag 的 java 类。 Later when we try to use it in java we declared the variable like below稍后当我们尝试在 java 中使用它时,我们声明了如下变量
Tag tag; //didn't worked
and Java class as below和Java类如下
@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!我们将变量名称从“tag”更改为“Tag”和 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. Java 类为 TagNew,变量名称为 tagNew,它工作正常。
@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. JaxB 如何使用命名约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.