2012-08-15 19 views
6

http POSTの実行中にレスポンスをStringレスポンスとして格納します。httppostからのxmlの解析response

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

私は文字列としてだけでセッションIDを保存したいと思います:私は応答を印刷する場合

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

は、それは次のようになります。私は

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

答えて

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

+0

私はちょうどあなたがレスポンスに何が得られるのか分かりませんが、UTF文字列の先頭にBOM(詳細についてはGoogle XML BOM)が含まれている可能性があります。 Eclipseのようなデバッガをお持ちの場合は、ハードコーディングされた作業文字列との違いを把握するのは簡単です。 –

+0

レスポンスのテキストエンコーディングがUTF-8の場合、おそらくレスポンス= EntityUtils.toString(resEntity)を変更する価値があります。レスポンス= EntityUtils.toString(resEntity、 "UTF-8"); –

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