0

这是我第一次尝试解析 xml 文件,所以我需要一些帮助来了解如何从我的 android 应用程序中的 xml 字符串中获取我需要的所有信息。这是我的 xml 字符串:

<ASX VERSION="3.0"><TITLE>Hits80and90.Com</TITLE><ENTRY>        <TITLE>Hits 80s and 90s</TITLE>       <REF HREF= "http://server-uk3.radioseninternetuy.com:11168"/>      <AUTHOR>Hits80and90.Com</AUTHOR>      <Abstract>The Best Music On The Web</ABSTRACT>       </ENTRY></ASX>

如何获取<ENTRY>标题、链接等标签中的所有信息?

感谢您提供任何帮助!

4

2 回答 2

0

这是您可以用来解析 XML String 的内容:

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class AsxParser {

    XmlPullParser xmlpullparser;

    public void AscParser(){

    }

    public void parsing(String url){
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            xmlpullparser = factory.newPullParser();
            xmlpullparser.setInput(new StringReader(url));

            int eventType = 0;  
            try {  
                eventType = xmlpullparser.getEventType();  
            } catch (XmlPullParserException e) {  
                e.printStackTrace();  
            }  
            while (eventType != XmlPullParser.END_DOCUMENT) {  

                parseTag(eventType);  
                try {  
                    eventType = xmlpullparser.next();  
                } catch (XmlPullParserException e) {  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }


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

    void parseTag(int event){  

        switch (event) {  

        case XmlPullParser.START_DOCUMENT:  
            Log.i("","START_DOCUMENT");  
            break;  

        case XmlPullParser.END_DOCUMENT:  
            Log.i("","END_DOCUMENT");  
            break;  
        case XmlPullParser.START_TAG:  
            Log.i("","START_TAG : "+xmlpullparser.getName());  
            Log.i("","Attribute Name : "+xmlpullparser.getAttributeValue(null,"HREF"));  

            break;  

        case XmlPullParser.END_TAG:
            Log.i("","END_TAG : "+xmlpullparser.getName());
            break;

        case XmlPullParser.TEXT:
            Log.i("","TEXT");
            String output = xmlpullparser.getText();  
            Log.i("valuee : ",""+output);  
            break;  
        }  

    }  

}
于 2012-05-19T11:24:02.060 回答
0

您可以使用 sax 解析器,我编写了一小段代码来向您展示如何获取条目中的标题。我没有测试它,但我认为它会帮助你用 sax 解析 XML 文件。

这里的代码:

import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParserContent {

public static void parse(String data) {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SaxParserContent.HandlerContent handler = new HandlerContent();
    try {
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new InputSource(new StringReader(data)), handler);

    } catch (Throwable err) {
    }
}

public static class HandlerContent extends DefaultHandler {

    protected boolean inEntry = true;
    protected boolean inTitle = true;
    protected String title;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (localName.equals("ENTRY")) {
            inEntry = true;
        }
        if (inEntry == true && localName.equals("TITLE")) {
            inTitle = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equals("ENTRY")) {
            inEntry = false;
        }
        if (inEntry == true && localName.equals("TITLE")) {
            inTitle = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (inEntry == true && inTitle == true) {
            title = new String(ch);
        }
    }

    public String getTitle() {
        return title;
    }
}

}

祝你好运 !

于 2012-05-19T08:23:04.313 回答