使用SAXParser解析不正确

时间:2013-08-20 09:44:54

标签: android saxparser

当我解析我的xml文件时,在第一个循环上它没关系,但是当它再次循环时,标记“duration”的值写入“Link”字段。 我看到变量elementOn没有得到真正的价值。

以下是我的应用程序的三个类:

SAXParserActivity:

public class SAXParserActivity extends Activity {

XMLGettersSetters data;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /**
     * Get the view of the layout within the main layout, so that we can add
     * TextViews.
     **/
    View layout = findViewById(R.id.layout);

    /**
     * Create TextView Arrays to add the retrieved data to.
     **/
    TextView artist[];

    TextView trackname[];

    TextView file[];

    TextView duration[];

    TextView buyLink[];

    TextView plays[];

    TextView song_id[];

    try {

        /**
         * Create a new instance of the SAX parser
         **/
        SAXParserFactory saxPF = SAXParserFactory.newInstance();
        SAXParser saxP = saxPF.newSAXParser();
        XMLReader xmlR = saxP.getXMLReader();

        // URL url = new
        // URL("http://www.xmlfiles.com/examples/cd_catalog.xml"); // URL of
        // the XML
        URL url = new URL(
                "http://artistecard.com/api/get_musics/18845.xml?source=iphone"); // URL
                                                                                    // of
                                                                                    // the
                                                                                    // XML

        /**
         * Create the Handler to handle each of the XML tags.
         **/
        XMLHandler myXMLHandler = new XMLHandler();
        xmlR.setContentHandler(myXMLHandler);
        xmlR.parse(new InputSource(url.openStream()));

    } catch (Exception e) {
        System.out.println(e);
    }

    data = XMLHandler.data;

    /**
     * Makes the TextView length the size of the TextView arrays by getting
     * the size of the
     **/
    artist = new TextView[data.getArtist().size()];
    trackname = new TextView[data.getTrackname().size()];
    file = new TextView[data.getFile().size()];
    duration = new TextView[data.getDuration().size()];
    buyLink = new TextView[data.getBuyLink().size()];
    plays = new TextView[data.getPlays().size()];
    song_id = new TextView[data.getSong_id().size()];

    /**
     * Run a for loop to set All the TextViews with text until the size of
     * the array is reached.
     * 
     **/
    for (int i = 0; i < data.getArtist().size(); i++) {

        artist[i] = new TextView(this);
        artist[i].setText("Artist = " + data.getArtist().get(i));

        trackname[i] = new TextView(this);
        trackname[i].setText("Trackname = " + data.getTrackname().get(i));

        file[i] = new TextView(this);
        file[i].setText("file = " + data.getFile().get(i));

        duration[i] = new TextView(this);
        duration[i].setText("Duration = " + data.getDuration().get(i));

        buyLink[i] = new TextView(this);
        buyLink[i].setText("BuyLink = " + data.getBuyLink().get(i));

        plays[i] = new TextView(this);
        plays[i].setText("Plays = " + data.getPlays().get(i));

        song_id[i] = new TextView(this);
        song_id[i].setText("Song_id = " + data.getSong_id().get(i));

        ((ViewGroup) layout).addView(artist[i]);
        ((ViewGroup) layout).addView(trackname[i]);
        ((ViewGroup) layout).addView(file[i]);
        ((ViewGroup) layout).addView(duration[i]);
        ((ViewGroup) layout).addView(buyLink[i]);
        ((ViewGroup) layout).addView(plays[i]);
        ((ViewGroup) layout).addView(song_id[i]);
    }

    setContentView(layout);

}

}

XMLHandler:

public class XMLHandler extends DefaultHandler {

String elementValue = null;
Boolean elementOn = false;
public static XMLGettersSetters data = null;

public static XMLGettersSetters getXMLData() {
    return data;
}

public static void setXMLData(XMLGettersSetters data) {
    XMLHandler.data = data;
}

/**
 * This will be called when the tags of the XML starts.
 **/
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    elementOn = true;

    // if (localName.equals("CATALOG"))
    if (localName.equals("album")) {
        data = new XMLGettersSetters();
        // } else if (localName.equals("CD")) {
    } else if (localName.equals("song")) {
        /**
         * We can get the values of attributes for eg. if the CD tag had an
         * attribute( <CD attr= "band">Akon</CD> ) we can get the value
         * "band". Below is an example of how to achieve this.
         * 
         * String attributeValue = attributes.getValue("attr");
         * data.setAttribute(attributeValue);
         * 
         * */
    }
}

/**
 * This will be called when the tags of the XML end.
 **/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    elementOn = false;

    /**
     * Sets the values after retrieving the values from the XML tags
     * */
    /*if (localName.equalsIgnoreCase("title"))
        data.setTitle(elementValue);
    else if (localName.equalsIgnoreCase("artist"))
        data.setArtist(elementValue);
    else if (localName.equalsIgnoreCase("country"))
        data.setCountry(elementValue);
    else if (localName.equalsIgnoreCase("company"))
        data.setCompany(elementValue);
    else if (localName.equalsIgnoreCase("price"))
        data.setPrice(elementValue);
    else if (localName.equalsIgnoreCase("year"))
        data.setYear(elementValue);*/

    if (localName.equalsIgnoreCase("artist"))
        data.setArtist(elementValue);
    else if (localName.equalsIgnoreCase("trackname"))
        data.setTrackname(elementValue);
    else if (localName.equalsIgnoreCase("file"))
        data.setFile(elementValue);
    else if (localName.equalsIgnoreCase("duration"))
        data.setDuration(elementValue);
    // elementOn=true;}
    else if (localName.equalsIgnoreCase("buyLink")) {
        // elementOn=false;
        data.setBuyLink(elementValue);
    } else if (localName.equalsIgnoreCase("plays"))
        data.setPlays(elementValue);
    else if (localName.equalsIgnoreCase("song_id"))
        data.setSong_id(elementValue);
    else if (localName.equalsIgnoreCase("duration"))
        data.setDuration(elementValue);
}

/**
 * This is called to get the tags value
 **/
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    if (elementOn) {
        elementValue = new String(ch, start, length);
        elementOn = false;
    }

}

}

XMLGettersSetters:

public class XMLGettersSetters {


private ArrayList<String> artist = new ArrayList<String>();
private ArrayList<String> trackname = new ArrayList<String>();
private ArrayList<String> file = new ArrayList<String>();
private ArrayList<String> duration = new ArrayList<String>();
private ArrayList<String> buyLink = new ArrayList<String>();
private ArrayList<String> plays = new ArrayList<String>();
private ArrayList<String> song_id = new ArrayList<String>();

public ArrayList<String> getArtist() {
    return artist;
}

public void setArtist(String artist) {
    this.artist.add(artist);
    Log.i("This is the artist:", artist);
}

public ArrayList<String> getTrackname() {
    return trackname;
}

public void setTrackname(String trackname) {
    this.trackname.add(trackname);
    Log.i("This is the trackname:", trackname);
}

public ArrayList<String> getFile() {
    return file;
}

public void setFile(String file) {
    this.file.add(file);
    Log.i("This is the file:", file);
}

public ArrayList<String> getDuration() {
    return duration;
}

public void setDuration(String duration) {
    this.duration.add(duration);
    Log.i("This is the duration:", duration);
}

public ArrayList<String> getBuyLink() {
    return buyLink;
}

public void setBuyLink(String buyLink) {
    this.buyLink.add(buyLink);
    Log.i("This is the Link:", buyLink);
}

public ArrayList<String> getPlays() {
    return plays;
}
public void setPlays(String plays) {
    this.plays.add(plays);
    Log.i("This is the plays:", plays);}

public ArrayList<String> getSong_id() {
    return song_id;
}
public void setSong_id(String song_id) {
    this.song_id.add(song_id);
    Log.i("This is the song_id:", song_id);
}

/*public ArrayList<String> getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company.add(company);
    Log.i("This is the company:", company);
}

public ArrayList<String> getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price.add(price);
    Log.i("This is the price:", price);
}

public ArrayList<String> getYear() {
    return year;
}

public void setYear(String year) {
    this.year.add(year);
    Log.i("This is the year:", year);
}

public ArrayList<String> getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title.add(title);
    Log.i("This is the title:", title);
}

public ArrayList<String> getArtist() {
    return artist;
}

public void setArtist(String artist) {
    this.artist.add(artist);
    Log.i("This is the artist:", artist);
}

public ArrayList<String> getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country.add(country);
    Log.i("This is the country:", country);
}*/

}

这是我的日志:

08-20 12:40:56.575: I/This is the artist:(5184): Патриція
08-20 12:40:56.575: I/This is the trackname:(5184): Тримай тепло
08-20 12:40:56.575: I/This is the file:(5184): /music/file/27244/01.Patricia_-trymay_teplo.mp3    
08-20 12:40:56.575: I/This is the duration:(5184): 04:17
08-20 12:40:56.575: I/This is the Link:(5184): https://itunes.apple.com/us/album/lecu/id651136316
08-20 12:40:56.575: I/This is the plays:(5184): 66
08-20 12:40:56.575: I/This is the song_id:(5184): 27244
08-20 12:40:56.575: I/This is the artist:(5184): Патриція
08-20 12:40:56.575: I/This is the trackname:(5184): Дорса
08-20 12:40:56.575: I/This is the file:(5184): /music/file/27246/02.Patricia_-_dorsa.mp3
08-20 12:40:56.575: I/This is the duration:(5184): 02:53
08-20 12:40:56.575: I/This is the Link:(5184): 02:53
08-20 12:40:56.575: I/This is the plays:(5184): 21
08-20 12:40:56.575: I/This is the song_id:(5184): 27246
08-20 12:40:56.575: I/This is the artist:(5184): Патриція
08-20 12:40:56.575: I/This is the trackname:(5184): Я здамся тобі
08-20 12:40:56.575: I/This is the file:(5184): /music/file/27247/03.Patricia_-_ya_zdamsja_tobi.mp3
08-20 12:40:56.575: I/This is the duration:(5184): 03:28
08-20 12:40:56.575: I/This is the Link:(5184): 03:28
08-20 12:40:56.575: I/This is the plays:(5184): 19
08-20 12:40:56.575: I/This is the song_id:(5184): 27247
08-20 12:40:56.575: I/This is the artist:(5184): Патриція
08-20 12:40:56.575: I/This is the trackname:(5184): Пам’ятай
08-20 12:40:56.575: I/This is the file:(5184): /music/file/27248/04.Patricia_-_pamyataj.mp3
08-20 12:40:56.575: I/This is the duration:(5184): 03:45
08-20 12:40:56.575: I/This is the Link:(5184): 03:45
08-20 12:40:56.575: I/This is the plays:(5184): 16
08-20 12:40:56.575: I/This is the song_id:(5184): 27248
08-20 12:40:56.575: I/This is the artist:(5184): Патриція
08-20 12:40:56.575: I/This is the trackname:(5184): Навіки живе
08-20 12:40:56.575: I/This is the file:(5184): /music/file/27249/05.Patricia_-_naviku_Zhuve.mp3
08-20 12:40:56.575: I/This is the duration:(5184): 03:57
08-20 12:40:56.575: I/This is the Link:(5184): 03:57
08-20 12:40:56.575: I/This is the plays:(5184): 17
08-20 12:40:56.575: I/This is the song_id:(5184): 27249

0 个答案:

没有答案