Jaxb 使用MOXy解组到不同的对象

Jaxb 使用MOXy解组到不同的对象,jaxb,eclipselink,moxy,unmarshalling,Jaxb,Eclipselink,Moxy,Unmarshalling,我有一个根元素为SearchResourceResponse的响应xml。我需要将其解组到另一个定制(HSIDetails)对象中。 我下面的意思正确吗??莫西能做到这一点吗 jaxbcontextjc=JAXBContext.newInstance(HSIDetails.class); Unmarshaller Unmarshaller=jc.createUnmarshaller(); HSIDetails HSIDetails=(HSIDetails)unmarshaller.unmarsh

我有一个根元素为SearchResourceResponse的响应xml。我需要将其解组到另一个定制(HSIDetails)对象中。 我下面的意思正确吗??莫西能做到这一点吗

jaxbcontextjc=JAXBContext.newInstance(HSIDetails.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
HSIDetails HSIDetails=(HSIDetails)unmarshaller.unmarshal(新StreamSource(新StringReader(responseXml));
还有我的课堂

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共类实现了可序列化{
/**
* 
*/
私有静态最终长serialVersionUID=-4352912510533245455L;
@XmlPath(“SearchResponseDetails/LogicalDevice/LogicalPhysicalResource/PhysicalResource[@*[local-name()='type'并包含(,'icl:Slot')]]/commonName”)
专用串槽;
@XmlPath(“SearchResponseDetails/LogicalDevice/LogicalPhysicalResource/PhysicalResource[@*[local-name()='type'并包含(,'icl:PhysicalPort')]]/commonName”)
专用字符串端口;
@XmlPath(“SearchResponseDetails/SubNetwork/Pipe/commonName”)
专用串电话;
@XmlPath(“//SearchResponseDetails/SubNetwork/Pipe/lrStatus”)
私有字符串状态;
公共字符串getSlot(){
返回槽;
}
公共无效设置批次(字符串插槽){
this.slot=slot;
}
公共字符串getPort(){
返回端口;
}
公共无效设置端口(字符串端口){
this.port=端口;
}
公共字符串getTelephone(){
回电;
}
公用电话(串电话){
这个电话;
}
公共字符串getLrStatus(){
返回状态;
}
公共void setLrStatus(字符串lrStatus){
this.lrStatus=lrStatus;
}
}
我的xml的一部分是:


XXXXX
1234567890
512/2
YYYY

您可以使用一个unmarshal方法,该方法采用
参数来告诉MOXy或任何JAXB(JSR-222)实现要解组的类

jaxbcontextjc=JAXBContext.newInstance(HSIDetails.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
HSIDetails HSIDetails=unmarshaller.unmarshal(
新StreamSource(新StringReader(responseXml)),
.class.getValue();

谢谢@Blaise Doughan,它确实有效,但值为空。我在包信息中添加了名称空间,如下所示,并更新了我的XPath。包信息“@javax.xml.bind.annotation.XmlSchema(namespace=”“,xmlns={@javax.xml.bind.annotation.xmlns(namespaceURI=“”,prefix=“ns2”)})”和我的xpath看起来像“@XmlPath”(@ns2:searchResourceResponse/SearchResponseDetails/SubNetwork/Pipe/CommonName/text()”)私有字符串电话”;我就是不能让它为上面的xml工作,我遗漏了什么吗?不管我给出什么xmlpath,空值都会出现。我相信问题不在于注释,而在于其他方面。@user2092307-你确定MOXy是作为JAXB提供者引入的吗。如果执行
System.out.println(jc.getClass())你得到了什么?我看到了:com.sun.xml.bind.v2.runtime.JAXBContextImpl类。我使用maven作为我的构建工具,因此我将jaxb.properties保存在src/main/resources下,它应该位于类路径中。我错了吗?@user2092307-这意味着您得到了JAXB参考实现。下面的例子应该会有所帮助: