2012-09-05 3 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

당신은 'STR'구문 분석하려고이

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

같은 몇 가지 일을하려고 할 수 있습니다 그것은 작동합니다.

여기에 dom 파서가 포함 된 순수한 샘플 구현이 있습니다.

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

관련 문제