2012-09-05 74 views
1

我正在使用Android,並且需要從URL獲取XML並檢索一些值。下載是好的,但有些字段可以包含HTML實體(如–)。當我從Node類(org.w3c.dom.Node)中調用方法getNodeValue()時,該值在找到& char時停止,並截斷字符串。getNodeValue()截斷org.w3c.dom.Node中的屬性內容

例如爲:

<title>Episode #56 &#8211; Heroes</title> 

當我打電話的getNodeValue()只返回 「情節#56」。

回答

0

,你可以嘗試一些這樣的事

String str = "<title>Episode #56 &#8211; Heroes</title>"; 
str = str.replaceAll("&", "amp;"); 

然後嘗試解析「STR」它應該工作。

這裏是pure with dom parser的示例實現。

public static void main(String[] args) throws XPathExpressionException { 
    String str = "<title>Episode #56 &#8211; Heroes</title>"; 
    str = str.replaceAll("&", "amp;"); 
    Document domDoc = null; 
    try { 
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
     ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes()); 
     domDoc = docBuilder.parse(bis); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    NodeList nlist = domDoc.getElementsByTagName("title"); 
    //System.out.println("child count "+nlist.getLength()); 
    System.out.println("title value = "+nlist.item(0).getTextContent()); 
} 
+0

我試過了,但沒有工作:String title = parser.getValue(e,KEY_TITLE); myObj.setTitle(title.replaceAll(「&」,「amp;」)); // 同樣的問題。 –

+0

@Rafael它的工作我有一個給定的示例代碼,但它沒有用dalvic運行時進行測試,但邏輯可以實現對嗎? – Sark