2011-12-29 12 views
0

Androidのxmlの内部コンテンツを取得する方法は?事前Androidの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が(存在する場合、私は2つの機能、の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"); 
関連する問題