简体   繁体   English

Android在线XML解析问题 - SAX Parser

[英]Android online XML parsing issue - SAX Parser

I am just starting out in android app development although I do have some knowledge of java. 我刚刚开始在Android应用程序开发中,虽然我对java有一些了解。

So my app needs both local & internet based xml files to be parsed. 所以我的应用程序需要解析local & internet based xml files I did manage to parse them locally but I have been trying for quite some time to parse the xml files on internet but no success. 我确实设法在本地解析它们,但我一直在尝试解析互联网上的xml文件,但没有成功。

I have used both XML Pull Parser & SAX Parser . 我使用了XML Pull Parser & SAX Parser Moreover, I have also tried multiple XML files but no progress. 此外,我还尝试了多个XML文件但没有进展。 I am posting a sample code of SAX Parser consisting of one of the sample XML files on a url. 我发布了一个SAX Parser的示例代码,该代码由一个url上的一个示例XML文件组成。

In the program, all I am trying to do is to just read a simple attribute from an element but the file can not be read. 在程序中,我所要做的就是从元素中读取一个简单的属性,但是无法读取该文件。

import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;


public class WeatherXMLParsing extends Activity 
{
TextView tv;

static final String baseUrl="http://gunsnroses23.zxq.net/ak/catalog.xml";
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_weather);
    tv = (TextView) findViewById(R.id.tv1);


    try
    {
        URL website= new URL(baseUrl);

        //setting up XMLReader & SAXParser to parse data
        SAXParserFactory spf=SAXParserFactory.newInstance();
        SAXParser sp=spf.newSAXParser();
        XMLReader xr=sp.getXMLReader();
        HandlingXMLStuff doingWork= new HandlingXMLStuff();
        xr.setContentHandler(doingWork);
        xr.parse(new InputSource(website.openStream()));
        String information=doingWork.getinformation();
        tv.setText(information);
    }
    catch (Exception e)
    {
        tv.setText("Error");
    }
  }
}

public class XMLDataCollected {

String catalog=null;


public void setCatalog(String c){
    catalog=c;
}
public String datatoString(){
    return "The attribute of catalog is " +catalog;
}
}

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HandlingXMLStuff extends DefaultHandler 
{
private XMLDataCollected info=new XMLDataCollected();
public String getinformation(){
    return info.datatoString();
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException 
        {
    // TODO Auto-generated method stub
        if (localName.equals("catalog")){
            String catalog=attributes.getValue("journal");
            info.setCatalog(catalog);
        }
    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

Update: 更新:

I want to thank you to all those who replied to my query. 我要感谢所有回复我查询的人。 Thank you so much but the problem has been resolved. 非常感谢你,但问题已经解决了。 There was no problem with the code. 代码没有问题。 The only simple problem was the "internet" permission was not defined in AndroidManifest.xml. 唯一的简单问题是AndroidManifest.xml中没有定义“互联网”权限。 That is what I have been thinking after going through the code again & again. 这是我一次又一次地完成代码之后的想法。 I could not find any issue since it is such a simple code but thank you, everyone. 我找不到任何问题,因为它是如此简单的代码,但谢谢大家。

I think what you are doing here is from the start wrong. 我认为你在这里做的是从一开始就错了。 I think the best way to achieve what you want is to "Make an HTTP request with android", and after you'll get response string, you will parse it like a local one. 我认为实现你想要的最好的方法是“用android发出一个HTTP请求”,然后在你得到响应字符串后,你会像本地一样解析它。 See this post how to do a http request: 看到这篇文章如何做一个http请求:

Make an HTTP request with android 用android发出HTTP请求

URL url = new URL("http://www.cricinfo.com/rss/content/story/feeds/6.xml");//parsing a sample url
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {

    if (xpp.getName().equalsIgnoreCase("item")) {//first tag is item
        insideItem = true;
    }
}
}

 public InputStream getInputStream(URL url) {  //open connection to url
       try {
           return url.openConnection().getInputStream();
       } catch (IOException e) {
           return null;
         }

This certainly worked for me for parsing rss feed xml document 这对于解析rss feed xml文档肯定有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM