XML转POJO JAVA

3

我已经阅读了其他与此问题相关的帖子,但是我无法解决我的问题。 我尝试将以下XML字符串转换为JAVA类,但是当我尝试使用getParam1()方法访问param1时,它返回null,并且我不确定原因。

XML字符串:

<?xml version="1.0" encoding="utf-8"?>
<REQUERYTRXRESPONSE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
 <param1>3gbahtJf1y85Oks4HrPLkqTQZV8Yg8pIhdXOrZ8pLGJP3FLwqKlIzIl/GgUpGvFaw4MC4SV+4pCudmVq+apIMIJJS4PrVyUx4T0ZO/Tsui4ZqCn62dLAG0DVhBVz2ZasF4yr7CRYnk47FWS0RywXmA==</param1>
 <param2>lO4ismiJwsvBiHQGW/UwCA==</param2>
 <param3 />
</REQUERYTRXRESPONSE>

Java 类:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(namespace = "http://tempuri.org/", name =  "REQUERYTRXRESPONSE")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class REQUERYTRXRESPONSE {
private String param1;
private String param2;
private String param3;

@XmlElement(required = true, name = "param1")
public String getParam1() {
    return param1;
}
public void setParam1(String param1) {
    this.param1 = param1;
}

@XmlElement(required = true, name = "param2")
public String getParam2() {
    return param2;
}
public void setParam2(String param2) {
    this.param2 = param2;
}

@XmlElement(required = true, name = "param3")
public String getParam3() {
    return param3;
}
public void setParam3(String param3) {
    this.param3 = param3;
}
}

XML转Java类代码:
HttpRequest httpRequest = HttpRequest.get();

    if (httpRequest.ok()) {
        String response = httpRequest.body();

        System.out.println(response);

        JAXBContext jaxbContext = JAXBContext.newInstance(REQUERYTRXRESPONSE.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        REQUERYTRXRESPONSE requerytrxresponse = (REQUERYTRXRESPONSE) unmarshaller.unmarshal(new StringReader(response));


        System.out.println((String) requerytrxresponse.getParam1()); // returns null
    }   

3
你能否将响应打印到控制台以检查它是否确实具有正确的XML内容?这将是我首先要检查的事情。 - Tim Biegeleisen
我认为你需要注释一下setter方法。 - shmosel
@shmosel 我不这么认为。 - Maurice Perry
@TimBiegeleisen 上面的 XML 字符串是将响应打印到控制台的结果。我从控制台复制它。 - Roham Amini
只是一个提示:在将来,简单地从上下文中创建一个marshaller,从您的类中实例化一个对象,将其编组到一个文件中。这样,您就可以看到您的类定义的XML,并从那里处理差异。 - Florian Schaetz
2个回答

7

终于搞明白了。

@XmlRootElement(name = "REQUERYTRXRESPONSE")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

   private String param1;
   private String param2;
   private String param3;

   public String getParam1() {
      return param1;
   }

   public void setParam1(String param1) {
      this.param1 = param1;
   }

   public String getParam2() {
      return param2;
   }

   public void setParam2(String param2) {
      this.param2 = param2;
   }

   public String getParam3() {
      return param3;
   }

   public void setParam3(String param3) {
      this.param3 = param3;
   }

在使用@XxmlAccessorType时,如果您不想要required=true,则不需要指定@XmlElement

我所做的更改是将命名空间从@XmlRootElement移至package-info.java类中:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://tempuri.org/",
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.sfatandrei.soplayground.model;

我的主要测试方法包括:

  final InputStream resourceAsStream = SoPlaygroundApplication.class.getClassLoader().getResourceAsStream("test.xml");
  JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  Response response = (Response) unmarshaller.unmarshal(resourceAsStream);
  System.out.println(response);

1
实际上,除非您想要非默认行为,否则根本不需要任何注释。 - Tim Biegeleisen
1
或者,您也可以将命名空间声明简单地复制到元素中。问题在于,您的类没有反映出XML:该类表示“元素没有命名空间”,但XML定义了所有内容都在http://tempuri.org/中。 - Florian Schaetz
您是正确的,@FlorianSchaetz,但从可读性和可维护性的角度来看,package-info.java 是更简单的方式。 - Andrei Sfat
@FlorianSchaetz 谢谢,非常有帮助。 - Javad Jafari

1

对我来说,它完全正常工作。确保您有适当的编码并检查您的jaxb提供程序。我使用默认的sun实现进行了测试 - com.sun.xml.bind.v2.runtime.JAXBContextImpl。

为您的取消编组代码进行测试:

@Test
public void testUnmarshaller() throws JAXBException, IOException {
    final InputStream expectedXmlResource = getClass().getResourceAsStream("/REQUERYTRXRESPONSE.xml");
    StringWriter stringWriter = new StringWriter();
    IOUtils.copy(expectedXmlResource, stringWriter, "UTF-8");

    JAXBContext jaxbContext = JAXBContext.newInstance(REQUERYTRXRESPONSE .class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    REQUERYTRXRESPONSE requerytrxresponse = (REQUERYTRXRESPONSE) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));

    assertEquals(requerytrxresponse.getParam1(), "3gbahtJf1y85Oks4HrPLkqTQZV8Yg8pIhdXOrZ8pLGJP3FLwqKlIzIl/GgUpGvFaw4MC4SV+4pCudmVq+apIMIJJS4PrVyUx4T0ZO/Tsui4ZqCn62dLAG0DVhBVz2ZasF4yr7CRYnk47FWS0RywXmA==");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接