2012-08-15 68 views
6

執行http POST期間,我將響應存儲爲字符串響應。從httppost響應中解析xml

HttpResponse httpresponse = httpclient.execute(httppost); 
HttpEntity resEntity = httpresponse.getEntity(); 
response = EntityUtils.toString(resEntity); 

如果我打印響應,它看起來像:

<?xml version="1.0" encoding="UTF-8"?> 
<response status="ok"> 
<sessionID>lo8mdn7bientr71b5kn1kote90</sessionID> 
</response> 

我想剛纔的SessionID存儲爲一個字符串。我試過

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
InputSource is = new InputSource(new StringReader(xml)); 

各種方法這樣,但它不會讓我跑的代碼,因爲DocumentBuildFactory和InputSource的是無效的。

我應該怎樣從XML中提取特定的字符串?

+0

我的[KSOAP2(http://ksoap2.sourceforge.net/)是處理這種反應 – mihail 2012-08-15 14:55:18

回答

8

這只是快速和骯髒的測試。它爲我工作。

import java.io.IOException; 
import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

public class Test { 
    public static void main(String[] args) { 
     String xml= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response status=\"ok\"><sessionID>lo8mdn7bientr71b5kn1kote90</sessionID></response>"; 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     InputSource is; 
     try { 
      builder = factory.newDocumentBuilder(); 
      is = new InputSource(new StringReader(xml)); 
      Document doc = builder.parse(is); 
      NodeList list = doc.getElementsByTagName("sessionID"); 
      System.out.println(list.item(0).getTextContent()); 
     } catch (ParserConfigurationException e) { 
     } catch (SAXException e) { 
     } catch (IOException e) { 
     } 
    } 
} 

輸出: lo8mdn7bientr71b5kn1kote90

+0

感謝的最好方式之一響應,這確實是唯一的問題,當我使用我的實際響應它沒有。我認爲這是因爲該字符串包含不正確轉義的引號。 – Ted 2012-08-15 17:30:12

+0

我不確定你在響應中究竟得到了什麼,有人猜測它可能在UTF字符串的開始處包含BOM(更多信息的Google XML BOM)。如果你有一個調試器(比如Eclipse中的調試器),應該非常直截了當地找出它和硬編碼工作字符串之間的區別。 – 2012-08-15 18:54:16

+0

如果響應的文本編碼是UTF-8,也許它也值得嘗試改變response = EntityUtils.toString(resEntity); to response = EntityUtils.toString(resEntity,「UTF-8」); – 2012-08-15 18:59:33

1

1.使用DOM Parser

如:

DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder odb = odbf.newDocumentBuilder(); 
      InputSource is = new InputSource(new StringReader(xml)); 
      Document odoc = odb.parse(is); 
      odoc.getDocumentElement().normalize(); // normalize text representation 
      System.out.println ("Root element of the doc is " + odoc.getDocumentElement().getNodeName()); 
      NodeList LOP = odoc.getElementsByTagName("locations"); 
      int totalPersons =LOP.getLength(); 
      System.out.println("Total nos of locations:"+totalPersons); 

      for(int s=0; s<LOP.getLength() ; s++) 
      { 
       Node FPN =LOP.item(s); 
       if(FPN.getNodeType() == Node.ELEMENT_NODE) 
        { 

        Element latlon = (Element)FPN;                 

        NodeList oNameList1 = latlon.getElementsByTagName("featured");          
        Element firstNameElement = (Element)oNameList1.item(0); 
        NodeList textNList1 = firstNameElement.getChildNodes(); 
        //this.setLocationId(((Node)textNList1.item(0)).getNodeValue().trim());  
        featuredArr = changeToBoolean(((Node)textNList1.item(0)).getNodeValue().trim());         // value taken 
        System.out.println("#####The Parsed data#####"); 
        System.out.println("featured : " + ((Node)textNList1.item(0)).getNodeValue().trim());   
        System.out.println("#####The Parsed data#####"); 
    } 

請參閱此鏈接瞭解詳情:

http://tutorials.jenkov.com/java-xml/dom.html