Java 使用JAXB但不获取值

Java 使用JAXB但不获取值,java,jaxb,Java,Jaxb,我试图使用下面的代码,但我没有得到值 main { String example="<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP:Body><ns0:findCustomerResponse xmlns:ns0=\"http://service.jaxws.blog/\"><return id=\"123\"><firstName&g

我试图使用下面的代码,但我没有得到值

main
{
String example="<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP:Body><ns0:findCustomerResponse xmlns:ns0=\"http://service.jaxws.blog/\"><return id=\"123\"><firstName>Jane</firstName><lastName>Doe</lastName></return></ns0:findCustomerResponse></SOAP:Body></SOAP:Envelope>";
ByteArrayInputStream bas=new ByteArrayInputStream(example.getBytes());
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SOAPMessage message = MessageFactory.newInstance().createMessage(null,
                bas);
        JAXBElement<Customer> jb = unmarshaller.unmarshal(message.getSOAPBody(), Customer.class);

        Customer customer = jb.getValue();
        System.out.println(customer.id);
        System.out.println(customer.firstName);
        System.out.println(customer.lastName);
      }

@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlAttribute
    int id;

    String firstName;

    String lastName;

}
main
{
字符串示例=“JaneDoe”;
ByteArrayInputStream bas=新的ByteArrayInputStream(例如.getBytes());
JAXBContext jc=JAXBContext.newInstance(Customer.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
SOAPMessage message=MessageFactory.newInstance().createMessage(null,
bas);
JAXBElement jb=unmarshaller.unmarshal(message.getSOAPBody(),Customer.class);
Customer=jb.getValue();
System.out.println(customer.id);
System.out.println(customer.firstName);
System.out.println(customer.lastName);
}
@XmlAccessorType(XmlAccessType.FIELD)
公共类客户{
@XmlAttribute
int-id;
字符串名;
字符串lastName;
}

请说明为什么我没有得到值

,正如@Andreas所说,客户嵌套在肥皂体的两个深处,因此如果您想得到它,您可以使用以下方法,但我认为它不太优雅:

JAXBElement<Customer> jb = unmarshaller.unmarshal(message.getSOAPBody().getFirstChild().getFirstChild(), Customer.class);
JAXBElement jb=unmarshaller.unmarshal(message.getSOAPBody().getFirstChild().getFirstChild(),Customer.class);

刚刚做了一个小改动:

取代

JAXBElement jb=unmarshaller.unmarshal(message.getSOAPBody(),Customer.class)

JAXBElement jb=unmarshaller.unmarshal(message.getSOAPBody().getFirstChild().getFirstChild(),Customer.class)

我得到了答案

System.out.println(customer.id); // printed 123
System.out.println(customer.firstName); // printed Jane
System.out.println(customer.lastName); // printed Doe

您需要为JAXB类中的所有变量定义getter和setter方法。还需要为firstName和lastName定义@XmlElement。@additionster否您不需要。
@xmlacessortype(xmlacesstype.FIELD)
注释意味着除非另有注释,否则所有字段都是隐式的
@xmlement
,JAXB将直接读取/写入字段值,而不使用getter和setter。为什么您希望
元素与
客户
类匹配?
id
属性位于
元素上,该元素嵌套两层,中间有一个
元素。@Andreas但是移除返回标记很好,但是我没有得到任何输出为0、null、null的值,但是预期的值是123、Jane、Doe。是否需要进行任何更改才能获得值?但是在这种情况下,只有123个值会出现,而其他值会出现。奇怪的是,我刚刚尝试过,它在我这边起作用。您是否使用您提供的相同代码片段进行了尝试?我已在包中设置了包信息,因此出现了一个问题,并在单独的包中获取了值。谢谢