2011-12-29 2 views
0

안드로이드에서 xml의 내부 내용을 얻는 방법? 사전안드로이드에서 xml의 내부 내용을 얻는 방법?

예를 들어 내가 다음 코드를 사용하고 있지만, 실제 결과를 반환하지 않는

경우 ..

String xml ="<hello><hai>welcome<hai></hello>"; 

InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));   
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
Document document = documentBuilder.parse(is);     
String response = document.getElementsByTagName("hello"); 

가 나는 결과를 필요로 "< 하이>에 오신 것을 환영합니다 < 하이>".. 감사합니다
+1

예 : http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html – Lumis

답변

0

저도 같은 문제를 겪고 난 이런 짓을 : 자식 노드가
및 xmltagswtf은() 코드

,369의 나머지를 반환있을 경우
나는이 개 기능은 GetXML() 검사를했다
public String getxml(Node b){ 
    String va = ""; 
    if (b.hasChildNodes()==true){ 
     int nodes = 0; 
     int a=b.getChildNodes().getLength(); 
     while (nodes <a){ 
      Node c = b.getChildNodes().item(nodes); 
      if(c.getNodeName()!="#text"){ 
      va+="<"+c.getNodeName()+">"+getxml(c)+"</"+c.getNodeName()+">"; 
      }else{ 
       va+=getxml(c); 
      }nodes+=1; 
     } 
    }else{va=b.getTextContent();} 
    return va; 

} 
public String xmltagswtf(String xmlt, String what) throws SAXException, IOException, ParserConfigurationException{ 
    InputStream is = new ByteArrayInputStream(xmlt.getBytes("UTF-8"));   
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
    Document document = documentBuilder.parse(is);     
    NodeList response = document.getElementsByTagName(what); 
    Node nod = response.item(0); 
    String nodos = ""; 
    if (nod.hasChildNodes()==true){ 

     int nodes = 0; 
     int a=nod.getChildNodes().getLength(); 
     while (nodes <a){ 
      Node c = nod.getChildNodes().item(nodes); 
      nodos+="<"+c.getNodeName()+">"+getxml(c)+"</"+c.getNodeName()+">"; 
      nodes+=1; 
     } 
    }else{ 
     nodos+=nod.getTextContent(); 
     System.out.println(nodos); 
    } 
    return nodos; 
} 

는 같은 것을 할 :

String xml ="<hello><hai>welcome</hai></hello>"; 
String hai =xmltagswtf(xml, "hello"); 
관련 문제