2012-09-20 83 views
0
"<?xml version=\"1.0\"?>" 
       + "<methodResponse>" 
       + "<fault>" 
       + "<value>" 
       + "<struct>" 
       + "<member>" 
       + "<name>faultCode</name>" 
       + "<value>" 
       + "<int>" 
       + "-1" 
       + "</int>" 
       + "</value>" 
       + "</member>" 
       + "<member>" 
       + "<name>faultString</name>" 
       + "<value>" 
       + "<string>" 
       + "The element type value must be terminated by the matching end-tag </value>" 
       + "</string>" + "</value>" + "</member>" + "</struct>" 
       + "</value>" + "</fault>" + "</methodResponse>"; 

在這裏,當我嘗試添加文本元素類型的值必須由匹配的結束標籤被終止,則拋出空指針異常。我該如何處理這個問題。我希望以下部分拋出錯誤。問題在於「結束標籤」。我該如何處理這個問題。印刷XML格式錯誤

+ "<string>" 
        + "The element type value must be terminated by the matching end-tag </value>" 
        + "</string>" + 

回答

3

你應該自己創建XML處理這個問題 - 你應該使用XML API。

您正在創建無效的XML,試圖將</value>作爲文本的一部分而不是轉義它。它應該是&lt;/value> - 但不是手動修復此問題,您應該在創建XML時使用XML API。

(該>可以可以逃到&gt;,但並不一定如此。)

2

你需要躲避文字...

"The element type value must be terminated by the matching end-tag &lt;/value&gt;" 
+0

我該如何轉義<>標記。我是否需要用<替換它還是有其他方法嗎? – BKK

+0

正如代碼示例所示,將「<」替換爲「<」和「>」替換爲「>」。您可能想閱讀[XML轉義](http://en.wikipedia.org/wiki/XML#Escaping) – MadProgrammer

+0

我會使用'String.replace'之類的東西,但正如Jon所說,我會也可以使用XML API – MadProgrammer