2011-01-03 10 views
2

Ich mache Verbindung mit meinem Web Service (SOAP) dieses xml Ergebnis, dass ich von den Web Service empfangen, wie kann ich dieses Ergebnis ohne SAX-Parser analysieren ...Android: Parsen von XML mit KSOAP

<maintag> 
<item> 
    <name>AndroidPeople</name> 
    <website category="android">www.androidpeople.com</website> 
</item> 
<item> 
    <name>iPhoneAppDeveloper</name> 
    <website category="iPhone">www.iphone-app-developer.com</website> 
    </item> 
</maintag> 

EDIT:/Ich habe mich gefragt, dieses Ergebnis mit Kxmlparser zu analysieren, kann mir jemand sagen, wie?

Vielen Dank!

SOAP FILE

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     tv = (TextView)findViewById(R.id.TextView01); 

     // Maak een nieuw Soap Request object en parameter 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

     request.addProperty("GUID","4fe78-a4s4df8-65a4sd-465as4a"); 
     request.addProperty("InstallVersion","1"); 

     // Soapenvelope versie van webservice 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 

     envelope.encodingStyle = SoapSerializationEnvelope.XSD; 
     envelope.setOutputSoapObject(request); 

     // Transport gegevens vanaf URL 
     HttpTransportSE aht = new HttpTransportSE(URL); 

     try 
     { 
      aht.call(SOAP_ACTION, envelope); 
      SoapPrimitive resultsString = (SoapPrimitive)envelope.getResponse(); 
      tv.setText("Result :" + resultsString); 
     } 

     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

Sie können auch prüfen, vtd-xml, die signifikant besser in vielerlei Hinsicht als SAX –

+0

Können Sie erkläre mir, wie ich mit diesem Parser arbeiten kann. – anddevelop

+0

Hallo, ich stehe vor dem gleichen Problem. I Soap-Webdienst gibt XML zurück.SoapPrimitive resultsString = (SoapPrimitive) envelope.getResponse(); SoapObject response = (SoapObject) envelope.getResponse() ;. Die obigen zwei Codes funktionieren nicht für mich. Nur Objekt funktioniert für mich. Kannst du mir eine Lösung vorschlagen? – Arun

Antwort

0

Java hat ein XML-Parser gebaut. Sie können eine Probe von einer aktuellen Datei sehe ich, dies zu tun gemacht hier: https://github.com/LeifAndersen/NetCatch/blob/master/src/net/leifandersen/mobile/android/netcatch/services/RSSService.java (es ist am Ende der Seite)

Hier sind die drei Methode der Sie meist interessieren:

private static Document getRSS(Context context, boolean backgroundUpdate, 
     String url) { 

    if (!Tools.checkNetworkState(context, backgroundUpdate)) 
     return null; 

    // Network is available get the document. 
    try { 
     Document doc; 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance() 
     .newDocumentBuilder(); 
     DefaultHttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(url); 
     HttpResponse response = client.execute(request); 
     doc = builder.parse(response.getEntity().getContent()); 
     return doc; 
    } catch (IOException e) { 
     return null; // The network probably died, just return null 
    } catch (SAXException e) { 
     // Problem parsing the XML, log and return nothing 
     Log.e("NCRSS", "Error parsing XML", e); 
     return null; 
    } catch (Exception e) { 
     // Anything else was probably another network problem, fail silently 
     return null; 
    } 
} 

private static Show getShowFromRSS(Context context, Document feed, 
     String feedUrl) { 
    try { 
     // There should be one channel in the feed, get it. 
     // Also, the cast should be okay if the XML is formatted correctly 
     NodeList item = feed.getElementsByTagName("channel"); 
     Element el = (Element)item.item(0); 

     String title; 
     NodeList titleNode = el.getElementsByTagName("title"); 
     if (titleNode == null || titleNode.getLength() < 1) 
      title = context.getString(R.string.default_title); 
     else 
      title = titleNode.item(0).getFirstChild().getNodeValue(); 

     String author; 
     NodeList authorNode = el.getElementsByTagName("author"); 
     if (authorNode == null || authorNode.getLength() < 1) 
      author = context.getString(R.string.default_author); 
     else 
      author = authorNode.item(0).getFirstChild().getNodeValue(); 

     String desc; 
     NodeList descNode = el.getElementsByTagName("comments"); 
     if (descNode == null || descNode.getLength() < 1) 
      desc = context.getString(R.string.default_comments); 
     else 
      desc = descNode.item(0).getFirstChild().getNodeValue(); 

     String imageUrl; 
     NodeList imagNode = el.getElementsByTagName("image"); 
     if(imagNode != null) { 
      Element ima = (Element)imagNode.item(0); 
      if (ima != null) { 
       NodeList urlNode = ima.getElementsByTagName("url"); 
       if(urlNode == null || urlNode.getLength() < 1) 
        imageUrl = null; 
       else 
        imageUrl = 
         urlNode.item(0).getFirstChild().getNodeValue(); 
      } else 
       imageUrl = null; 
     } else 
      imageUrl = null; 

     return new Show(title, author, feedUrl, desc, imageUrl, -1, -1); 
    } catch (Exception e) { 
     // Any parse errors and we'll log and fail 
     Log.e("NCRSS", "Error parsing RSS", e); 
     return null; 
    } 
} 

private static List<Episode> getEpisodesFromRSS(Context context, 
     Document feed) { 
    try { 
     ArrayList<Episode> episodes = new ArrayList<Episode>(); 
     NodeList items = feed.getElementsByTagName("item"); 
     for(int i = 0; i < items.getLength(); i++) { 
      // Fetch the elements 
      // Safe if it's an actual feed. 
      Element el = (Element)items.item(i); 

      String title; 
      NodeList titleNode = el.getElementsByTagName("title"); 
      if (titleNode == null || titleNode.getLength() < 1) 
       title = context.getString(R.string.default_title); 
      else 
       title = titleNode.item(0).getFirstChild().getNodeValue(); 

      String author; 
      NodeList authorNode = el.getElementsByTagName("author"); 
      if (authorNode == null || authorNode.getLength() < 1) 
       author = context.getString(R.string.default_author); 
      else 
       author = authorNode.item(0).getFirstChild().getNodeValue(); 

      String date; 
      NodeList dateNode = el.getElementsByTagName("pubDate"); 
      if (dateNode == null || dateNode.getLength() < 1) 
       date = context.getString(R.string.default_date); 
      else 
       date = dateNode.item(0).getFirstChild().getNodeValue(); 

      String desc; 
      NodeList descNode = el.getElementsByTagName("comments"); 
      if (descNode == null || descNode.getLength() < 1) 
       desc = context.getString(R.string.default_comments); 
      else 
       desc = descNode.item(0).getFirstChild().getNodeValue(); 

      String url; 
      NodeList urlNode = el.getElementsByTagName("enclosure"); 
      if (urlNode == null || urlNode.getLength() < 1) 
       url = ""; 
      else { 
       Element urlEl = (Element)urlNode.item(0); 
       if(urlEl == null) 
        url = ""; 
       else 
        url = urlEl.getAttribute("url"); 
      } 


      // Convert the date string into the needed integer 
      // TODO, use a non-depricated method 
      long dateMills; 
      try { 
       dateMills = Date.parse(date); 
      } catch (Exception e) { 
       dateMills = 0; 
      } 

      // Add the new episode 
      // ShowId and played doesn't really matter at this point 
      episodes.add(new Episode(title, author, desc, "", url, 
        dateMills, 0, false)); 
     } 
     return episodes; 

    } catch (Exception e) { 
     // Any parse errors and we'll log and fail 
     Log.e("NCRSS", "Error parsing RSS", e); 
     return null; 
    } 
} 
4

Abhängig von Ihrem Webservice erhalten Sie entweder ein SoapPrimitive oder ein SoapObject. Die meisten, wie es ist eine komplexere Reaktion und damit Ihr Code

SoapPrimitive resultsString = (SoapPrimitive)envelope.getResponse();

sollte mit Attributen und Eigenschaften von mit den Werten haben mit so etwas wie dieses

SoapObject response = (SoapObject)envelope.getResponse();

die wiederum ersetzt werden, um Ihre Antwort. Es könnte am einfachsten sein, dort einfach einen Haltepunkt zu setzen und ihn live zu inspizieren.

Sie können auch in meiner Wiki-Dokumentation sehen, wie die rohe XML-Anfrage und Antwort debuggen und hier zu sehen: http://code.google.com/p/ksoap2-android/wiki/CodingTipsAndTricks

+0

Hallo, ich stehe vor dem gleichen Problem. I Soap-Webdienst gibt XML zurück.SoapPrimitive resultsString = (SoapPrimitive) envelope.getResponse(); SoapObject response = (SoapObject) envelope.getResponse() ;. Die obigen zwei Codes funktionieren nicht für mich. Nur Objekt funktioniert für mich. Kannst du mir eine Lösung vorschlagen? – Arun