这是我第一次尝试解析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>
标签中的所有信息,如标题,链接等?
感谢您提供任何帮助!
答案 0 :(得分: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;
}
}
}
祝你好运!答案 1 :(得分:0)
这是您可以用来解析XML字符串的内容:
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;
}
}
}