Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Android XML file einlesen (https://www.delphipraxis.net/167910-android-xml-file-einlesen.html)

Cylence 25. Apr 2012 07:56

Android XML file einlesen
 
Hallo,

ich versuche für android einen rss parser umzubauen, so das er ergebnisse von tastekid,com anzeigt, das ist eine suche nach ähnlichen musik/video titeln. Aber ehrlich gesagt ich checks nich...

Mit podcast xmls gehts das perfekt ohne probleme, nur das xml ist etwas anders aufgebaut, erst einmal info resource dann results resource und ich bin wohl zu doof es hinzubekommen, ich bekomme zwar die richtige anzahl an ergebnissen, aber nur das erste hat auch einen titel sonst alles leer...

Das ist mein source:

Delphi-Quellcode:
    public static void parse(){
    URL url;
    try {
        url = new URL("http://www.tastekid.com/ask/ws?q=u2&verbose=1");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){           
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
              Document doc;
              doc = db.parse(url.openStream());
              doc.getDocumentElement().normalize();
              NodeList itemLst = doc.getElementsByTagName("resource"); //resource results
              Similiararrays.SimName = new String[itemLst.getLength()];
              Similiararrays.SimType = new String[itemLst.getLength()];
              Similiararrays.SimDescription = new String[itemLst.getLength()];
              Similiararrays.SimWikiURL = new String[itemLst.getLength()];  
              Similiararrays.SimSampleTitle = new String[itemLst.getLength()];
              Similiararrays.SimSampleURL = new String[itemLst.getLength()];  
              for(int i=0; i < itemLst.getLength()-1; i++){ 
                    Node item = itemLst.item(i);
                    if(item.getNodeType() == Node.ELEMENT_NODE){                       
                          Element ielem = (Element)item;          
                          NodeList SimName = ielem.getElementsByTagName("name");
                          NodeList SimType = ielem.getElementsByTagName("type");
                          NodeList description = ielem.getElementsByTagName("wTeaser");
                          NodeList media = ielem.getElementsByTagName("enclosure");                                                                              
                          Similiararrays.SimName[i] = SimName.item(0).getChildNodes().item(0).getNodeValue();
                          Similiararrays.SimType[i] = SimType.item(0).getChildNodes().item(0).getNodeValue();
                          Similiararrays.SimDescription[i] = description.item(0).getChildNodes().item(0).getNodeValue();
                          String WikiURL = media.item(0).getAttributes().getNamedItem("wUrl").getNodeValue();  
                          Similiararrays.SimWikiURL[i] = WikiURL;                            
                            String SampleTitle = media.item(0).getAttributes().getNamedItem("yTitle").getNodeValue();
                          Similiararrays.SimSampleTitle[i] = SampleTitle;
                          String sampleurl = media.item(0).getAttributes().getNamedItem("yUrl").getNodeValue();
                          Similiararrays.SimSampleURL[i] = sampleurl;
                    }                   
              }             
        }       
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (DOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }   
    }
daher kommt das original:
http://droidapp.co.uk/?p=166

und so ist das xml aufgebaut:


Delphi-Quellcode:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<similar>
    <info>
        <resource>
            <name><![CDATA[U2]]></name>
            <type>music</type>
                        <wTeaser><![CDATA[U2 are an Irish rock band from Dublin. Formed in 1976, the group consists of Bono (vocals and guitar), The Edge (guitar, keyboards and vocals), Adam Clayton (bass guitar), and Larry Mullen, Jr. (drums and percussion). U2's early sound was rooted in post-punk but eventually grew to incorporate influences from many genres of popular music. Throughout the group's musical pursuits, they have maintained a sound built on melodic instrumentals, highlighted by The Edge's textural guitar playing and Bono's expressive vocals. Their lyrics, often embellished with spiritual imagery, focus on personal themes and sociopolitical concerns.U2 formed at Mount Temple Comprehensive School when the members were teenagers with limited musical proficiency. Within four years, they signed with Island Records and released their debut album Boy.]]></wTeaser>
            <wUrl><![CDATA[http://en.wikipedia.org/wiki/U2_(music)]]></wUrl>
            <yTitle><![CDATA[u2- live - with or without you]]></yTitle>
            <yUrl><![CDATA[http://www.youtube.com/v/_Ye8GLPUVsM?version=3&f=videos&c=TasteKid&app=youtube_gdata]]></yUrl>
            <yID><![CDATA[_Ye8GLPUVsM]]></yID>
                                                                    </resource>
    </info>
    <results>
        <resource>
            <name><![CDATA[INXS]]></name>
            <type>music</type>
                        <wTeaser><![CDATA[INXS (pronounced "in excess", In-XS) are an Australian rock band, formed as The Farriss Brothers in 1977 in Sydney, New South Wales. Mainstays are Garry Gary Beers on bass guitar, Andrew Farriss on guitar/keyboards, Jon Farriss on drums, Tim Farriss on lead guitar and Kirk Pengilly on guitar/sax. For twenty years, they were fronted by Michael Hutchence on lead vocals, whose "sultry good looks" and magnetic stage presence made him the focal point of the band. Initially known for their New Wave/ska/pop style, they later developed a harder pub rock style, including funk and dance elements.In the early 1980s, INXS first charted in their native Australia with their debut self-titled album, but later garnered moderate success elsewhere with Shabooh Shoobah and a single, "The One Thing".]]></wTeaser>
            <wUrl><![CDATA[http://en.wikipedia.org/wiki/Inxs]]></wUrl>
            <yTitle><![CDATA[INXS - Never Tear Us Apart]]></yTitle>
            <yUrl><![CDATA[http://www.youtube.com/v/_VU9DjQpvMQ?version=3&f=videos&c=TasteKid&app=youtube_gdata]]></yUrl>
            <yID><![CDATA[_VU9DjQpvMQ]]></yID>
                                                                    </resource>      
    </results>
</similar>

Cylence 25. Apr 2012 10:41

AW: Android XML file einlesen
 
Ich habs endlich: so gehts:

Delphi-Quellcode:
   public static void parse(){
   URL url;
   try {
      url = new URL("http://www.tastekid.com/ask/ws?q=" + MRUtils.mSimiliarTitle + "&verbose");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){         
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
              Document doc;
              doc = db.parse(url.openStream());
              doc.getDocumentElement().normalize();                            
              NodeList XResultLst = doc.getElementsByTagName("similar");            
              Node XResultitem = XResultLst.item(0);
              NodeList ResultLst = ((Element) XResultitem).getElementsByTagName("results");
              Node Resultitem = ResultLst.item(0);
              NodeList itemLst = ((Element) Resultitem).getElementsByTagName("resource");
              Integer ResultsCount = itemLst.getLength();
              Similiararrays.SimName = new String[ResultsCount];
              Similiararrays.SimType = new String[ResultsCount];
              Similiararrays.SimDescription = new String[ResultsCount];
              Similiararrays.SimWikiURL = new String[ResultsCount];  
              Similiararrays.SimSampleTitle = new String[ResultsCount];
              Similiararrays.SimSampleURL = new String[ResultsCount];  
              for(int i=0; i < ResultsCount; i++){ 
                    Node item = itemLst.item(i);
                    if(item.getNodeType() == Node.ELEMENT_NODE){   
                          Element ielem = (Element)item;    
                          NodeList SimName = ielem.getElementsByTagName("name");
                          NodeList SimType = ielem.getElementsByTagName("type");
                          NodeList description = ielem.getElementsByTagName("wTeaser");
                          NodeList SimWikiURL = ielem.getElementsByTagName("wUrl");
                          NodeList SimSampleTitle = ielem.getElementsByTagName("yTitle");
                          NodeList SimSampleURL = ielem.getElementsByTagName("yUrl");
                          Similiararrays.SimName[i] = SimName.item(0).getChildNodes().item(0).getNodeValue();
                          Similiararrays.SimType[i] = SimType.item(0).getChildNodes().item(0).getNodeValue();
                          Similiararrays.SimDescription[i] = description.item(0).getChildNodes().item(0).getNodeValue();
                          Similiararrays.SimWikiURL[i] = SimWikiURL.item(0).getChildNodes().item(0).getNodeValue();    
                          Similiararrays.SimSampleTitle[i] =SimSampleTitle.item(0).getChildNodes().item(0).getNodeValue();
                          Similiararrays.SimSampleURL[i] = SimSampleURL.item(0).getChildNodes().item(0).getNodeValue();
                    }                   
              }             
        }      
   } catch (MalformedURLException e) {
      e.printStackTrace();
   } catch (DOMException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   } catch (ParserConfigurationException e) {
      e.printStackTrace();
   } catch (SAXException e) {
      e.printStackTrace();
   }   
   }


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:56 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz