XML file einlesen

C

Cylance

Neues Mitglied
0
Hi,

ich versuche ein XML file einzulesen und auszuwerten von tastekid.com das gibt mir ähnliche musiktitel zurück, leider check ich irgendwas nich ganz. Bei Podcasts gehts ohne Probleme aber hier hab ich einen spinner. Bestimmt isses nur ein kleiner fehler kann mir jemand helfen?

so les ich das ein:
Code:
    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();
    }    
    }
und das xml file das ich von tastekid bekomme sieht so aus:

Code:
<?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>
achja das Problem is ich habe so viele items wie es results gibt, das ist ok, aber keinerlei text kein name nichts wird in mein array gespeichert, ich denk da muß ich erstmal in results und da erst nach resource suchen...so in der art, hab haufenweise probiert, aber ich checks nich... keine ahnung...hiiilfe
 
Zuletzt bearbeitet:
SimName.item(0).getChildNodes().item(0).getNodeValue(); liefert dir die Kinder vom ersten Buchstaben im Namen, also nix.
Mit getAttributes() sucht man nach Attributen. Diese kommen in deinem XML File garnicht vor.
 
Zuletzt bearbeitet:
hmm ok, aber der selbe code geht bei video podcast xml files ohne probleme, nur eben dort nicht, eil das xml auch bischen anders aufgebaut ist, was muß ich ändern das es stimmt?

Das erste Item hat auch den Titel aus dem XML file also so weit stimmts noch nur alle weiteren Ergebnisse nicht

hier kam der original source her:

http://droidapp.co.uk/?p=166
 
Zuletzt bearbeitet:
ich habs hinbekommen

Code:
    public static void parse(){
    URL url;
    try {
        url = new URL("http://www.tastekid.com/ask/ws?q=" + MRUtils.mSimiliarTitle + "&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 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();
    }    
    }
 
Hi alle zusammen,

ich hab mal deinen Script ausprobiert und habe diverse Fehlermeldungen wegen der Imports bekommen.
Ich könnte jetzt Schritt für Schritt die einzelnen Meldungen durchgehen und für jeden einen weiteren Import machen, also: URL, HttpURLConnetcion, DocumentBuilderFactory... usw.
ODER gibt es da einen Import oder ein paar Imports die alle Fehlermeldungen abdecken würden?

Ich habe bisher nur die "Standart" Imports:
Code:
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

kann mir da wer helfen?
 
Eclipse macht das für dich: Auf die Fehlermeldung klicken, Quick Fix wählen
(und die richtige Klasse aussuchen, falls es mehrere gibt).
 
Dankeschön für die schnelle Antwort funktioniert alles super :thumbup:
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.902
swa00
swa00
M
  • myoggradio
Antworten
1
Aufrufe
776
myoggradio
M
H
  • hörnerwhisky
Antworten
1
Aufrufe
1.634
deek
D
Zurück
Oben Unten