2012-11-12 47 views
0

我正在調用的JAX-RS Web服務將xml內容作爲文本/ html內容類型拋出。在我這邊,我需要讀取xml,並將其轉換爲Java對象。帶格式錯誤的解組JAX-RS響應xml(content-type:text/xml)

問題是:響應xml格式不正確,在錯誤位置有換行符,例如 - 在<?xml version="1.0" encoding="UTF-8"?>之前有幾個換行符。這是試圖解開它的問題。

有沒有一種方法可以解組響應xml字符串,雖然它有格式化問題?

在此先感謝。

HttpGet httpGet = new HttpGet(uri); 
HttpResponse response = client.execute(httpGet); 

InputStream inputStream = response.getEntity().getContent(); 

JAXBContext context = JAXBContext.newInstance(MyClass.class); 
MyClass myObj = (MyClass) context.createUnmarshaller().unmarshal(inputStream); 
+0

您能否顯示與此事有關的代碼? – imwill

+0

@imwill:已添加。 – James

+0

裏面還有格式化問題嗎?或者在<?xml ..之前? – imwill

回答

0

終於得到了供應商發送的XML /應用格式的響應。然而,作爲一種選擇,我發現的一個選擇是將內容保存爲文件,然後再從文件中檢索內容。

0

如何解組之前微調內容:

import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.CharEncoding; 

MyClass myObj = MyClass.class.cast(
    context.createUnmarshaller().unmarshal(
    IOUtils.toInputStream(
     IOUtils.toString(
     InputStream.class.cast(response.getEntity().getContent()), 
     CharEncoding.UTF_8 
    ).trim(), 
     CharEncoding.UTF_8 
    ) 
) 
);