2012-08-15 17 views
6

Bir http POST uygulamasının yürütülmesi sırasında yanıt bir Dize yanıtı olarak saklanır.https giriş yanıtından xml ayrıştırılıyor

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

Ben bir dize olarak sadece SessionID saklamak istiyorum: Ben yanıtını yazdırırsanız

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

Göründüğü gibi. Ben

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

ve bunun gibi çeşitli yöntemler denedim ama DocumentBuildFactory ve InputSource geçersizdir çünkü beni kod çalıştırmasına izin vermeyecek.

Bu XML'den belirli dizeleri çıkarmak için ne yapmalıyım?

+0

[KSOAP2] (http://ksoap2.sourceforge.net/) için – mihail

cevap

8

Bu sadece hızlı ve kirli testtir. Benim için çalıştı.

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) { 
     } 
    } 
} 

çıkış: Benim için lo8mdn7bientr71b5kn1kote90

+0

Teşekkür tepkilerin bu tür işlemek için en iyi yollardan biridir yanıtı, bu sadece tek problemi çözer, gerçek cevabımı kullandığımda olmaz. Sanırım bu, dizenin düzgün bir şekilde kaçılmayan tırnak içerdiğini düşünüyorum. – Ted

+0

Yanıtta tam olarak ne aldığınızdan emin değilim, bir tahmin UTF dizesinin başında BOM (daha fazla bilgi için google XML BOM) içerebilir. Eğer bir hata ayıklayıcınız varsa (Eclipse'deki gibi), kodlanmış bir çalışma dizgisi ile aralarındaki farkın ne olduğunu anlamak için hemen düz olmalıdır. –

+0

Yanıtın metin kodlaması UTF-8 ise, belki de yanıtı değiştirmeye değerdir = EntityUtils.toString (resEntity); yanıt = EntityUtils.toString (resEntity, "UTF-8"); –

1

1.DOM Parser'u kullanın.

Örn:

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#####"); 
    } 

fazla ayrıntı için bu bağlantıya bakın:

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

İlgili konular