2017-08-14 75 views
2

我已閱讀與此問題有關的其他帖子,但我無法解決我的問題。我嘗試將以下XML字符串轉換爲JAVA類,但是當我嘗試使用getParam1()方法訪問param1時,它返回null,我不知道爲什麼。XML到POJO JAVA

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內容?這將是我會檢查的第一件事。 –

+0

我認爲你需要註釋setter。 – shmosel

+0

@shmosel我不這麼認爲 –

回答

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

我改變的是,我在一個package-info.java類移動的命名空間從@XmlRootElement像這樣:

@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

其實你不需要任何註釋,除非你想要非默認行爲。 –

+0

@TimBiegeleisen很高興知道。謝謝 – sfat

+1

或者,您也可以簡單地將名稱空間聲明覆制到元素中。問題是,你的類沒有反映xml:類說「元素沒有命名空間」,但xml定義了「http:// tempuri.org /'中的東西。 –

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=="); 
}