2012-07-11 77 views
-3
<string xmlns="http://tempuri.org/"> 
    <NewDataSet> 
    <myTable> 
     <USERNAME>admin</USERNAME> 
     <PASSWORD>123</PASSWORD> 
    </myTable> 
    <myTable> 
     <USERNAME>John</USERNAME> 
     <PASSWORD>678</PASSWORD> 
    </myTable> 
    </NewDataSet> 
    </string> 

如何解析从android的web服务返回的字符串?如何解析Android中的Web服务返回的字符串?

+0

你想做什么?你想摆脱它什么? – 2012-07-11 13:10:26

+0

你应该更具针对性。例如: 给服务器返回到你的android应用程序的字符串。 提供您想要的字符串中的“信息”。 – 2012-07-11 13:13:24

+0

阅读[解析XML数据](http://developer.android.com/training/basics/network-ops/xml.html) – adatapost 2012-07-11 13:16:13

回答

0
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
    InputSource is = new InputSource(); 
    is.setCharacterStream(new StringReader(xmlRecords)); 

    Document doc = db.parse(is); 
    NodeList nodes = doc.getElementsByTagName("employee"); 

    for (int i = 0; i < nodes.getLength(); i++) { 
     Element element = (Element) nodes.item(i); 

     NodeList name = element.getElementsByTagName("name"); 
     Element line = (Element) name.item(0); 
     System.out.println("Name: " + getCharacterDataFromElement(line)); 

     NodeList title = element.getElementsByTagName("title"); 
     line = (Element) title.item(0); 
     System.out.println("Title: " + getCharacterDataFromElement(line)); 
    } 

    } 

    public static String getCharacterDataFromElement(Element e) { 
    Node child = e.getFirstChild(); 
    if (child instanceof CharacterData) { 
     CharacterData cd = (CharacterData) child; 
     return cd.getData(); 
    } 
    return ""; 
    } 
} 
+0

谢谢..我会尝试修复我的代码,我希望它的工作.. – 2012-07-11 13:23:05

+0

希望它可以帮助:-)。当他们帮助时,不要忘记接受答案! – 2012-07-11 13:26:23