从互联网上解析XML(yr.no) [英] Parse xml from internet (yr.no)

查看:195
本文介绍了从互联网上解析XML(yr.no)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是XML解析,所以也许一些我写看起来愚蠢一些,也许我的一些术语不是很正确的事情经历。请原谅。

I am not experienced in xml parsing so maybe some of the things I write look stupid to some and maybe some of my terminology is not quite correct.. Please forgive.

我开发一个Android应用程序,其中包括一个需要解析来自 YR.no 的天气数据。该组织提供与提供XML格式的某些数据方法的API。比方说,比如我想从这个 HTTP解析XML数据:// API。 yr.no/weatherapi/seaapproachforecast/1.0/?location=stad

I develop an android app, which needs among others to parse weather data from YR.no. This organization offers an api with methods that provide certain data on xml format. Let’s say for example I want to parse xml data from this http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad

我开发了一个code,可以做一些XML解析和它的工作原理就在此 HTTP ://www.w3schools.com/xml/simple.xml (作为测试)。

I developed a code that can do some xml parsing and it works right in this http://www.w3schools.com/xml/simple.xml (as a test).

主要code线来定义我的BaseFeedParser类得到什么是:

The main code lines to define what to get in my BaseFeedParser class are:

RootElement root2 = new RootElement("breakfast_menu");
Element food = root2.getChild("food");
Element name = food.getChild("name");

food.setEndElementListener(new EndElementListener() {
    public void end() {
        messages.add(currentMessage.copy());
    }
});

food.getChild("name").setEndTextElementListener(new EndTextElementListener() {
    public void end(String body) {
        currentMessage.setTitle(body);
    }
});

try {
    Xml.parse(this.getInputStream(), Xml.Encoding.ISO_8859_1, root2.getContentHandler());
} catch (Exception e) {
    throw new RuntimeException(e);
}
return messages;

然后再从我的活动类:

And then from my activity class:

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    loadFeed();
}

private void loadFeed() {
    try {
        BaseFeedParser parser = new BaseFeedParser();
        messages = parser.parse();
        List<String> titles = new ArrayList<String>(messages.size());
        System.out.println(messages.size());
        for (Message msg : messages) {
            titles.add(msg.getTitle());
        }
        ArrayAdapter<String> adapter = 
                new ArrayAdapter<String>(this, R.layout.row,titles);
        this.setListAdapter(adapter);
        String str = "!";
        if (titles != null) {
            str = titles.toString();
            System.out.println("not null");
            System.out.println(str);
        }

        test(str);
    } catch (Throwable t) {
        Log.e("AndroidNews",t.getMessage(), t);
    }
}

public void test(String s) {
    setContentView(R.layout.error);
    TextView textView = (TextView) findViewById(R.id.mytextview); 
    textView.setText(s);
}

因此​​,它返回并打印我想要的数据(比利时华夫饼干等)

So it returns and prints the data I want ("Belgian Waffles" etc)

我与我本来想解析yr.no数据的问题是,每一个孩子到底不包含只是一个值,但可以有更多的标签(如&LT; waveDirection单位=度的价值=250/&GT; )。所以,当我改变的元素来使用这一个,它结束25个不同的字符串 S(其中如果算上都是不同的孩子与标记 waveDirection ),但每个值是空的(如字符串A =)。我没有得到任何错误,我只是得到了25空字符串列表。我试图达到我的元素的方法是这样的:

My problem with the yr.no data that I originally wanted to parse is that every end child does not contain just one value but can have more tags (e.g. <waveDirection unit="degree" value="250"/>). So when I change the elements to use this one, it ends to 25 different Strings (which if you count are all the different children with tag waveDirection) but every value is empty (like a String a = ""). I get no error, I just get a list of 25 empty strings. The way I try to reach my element is something like:

RootElement root = new RootElement("weatherdata");
Element product = root.getChild("product");
Element time = product.getChild("time");
Element location = time.getChild("location");

location .setEndElementListener(new EndElementListener(){
public void end() {
        messages.add(currentMessage.copy());            
    }
});

location.getChild("windDirection").setEndTextElementListener(new EndTextElementListener() {
    public void end(String body) {
        currentMessage.setTitle(body);
    }
});

所以,我应该怎么修改此,使其与该XML的作品?我不提供所有的类和方法(如的setTitle()),但我认为他们的工作,因为他们解析没错,我的第一次测试的XML。我想我把我的 feedUrlString =htt​​p://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad; ,因为它找到了正确的根的文件和25的元素

So how should I modify this so that it works with this xml? I do not provide all the classes and methods (like setTitle()) but I think they work since they parse right my first test xml. And I suppose I set my feedUrlString = "http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad"; correctly since it finds the root of the document and 25 elements.

编辑:我做到了!得到的属性正确的方法是使用:

I did it! The right way to get the attributes was to use:

    location.setStartElementListener(new StartElementListener(){
        public void start(Attributes attributes){
            messages.add(currentMessage.copy());            
        }
    });

    location.getChild("windDirection").setTextElementListener(new TextElementListener(){
    public void end(String body) {
        //currentMessage.setTitle(body);
        //System.out.println("b="+ body);
        }

    @Override
    public void start(Attributes attributes) {
            System.out.println("val" + attributes.getValue("deg"));
            currentMessage.setTitle(attributes.getValue("deg"));
        }
    });

所以,现在我得到我的数据,但由于某些原因以外的所有非常最后一个元素(我测试了其他YR.no XML的为好)..必须有,我应该解决但主要步骤做了一些错误。谢谢大家的答案,尤其是user306848谁向我指出我用!方向

So now I get my data but for some reason all except the very last element (I tested it for other YR.no xmls as well).. There must be some bug that I should solve but the major step is done. Thank you all for the answers and especially user306848 who pointed me to the direction I used!

推荐答案

我觉得你的code假定有是重新presents你所寻求的价值文本节点。这是不是来自yr.no域xml文件的情况。

I think your code assumes there is text node which represents the values you seek. Which is not the case for the xml file from the yr.no domain.

您需要弄清楚如何读取标签属性,您所使用的XML库。

You need to figure out how to read attributes from tags for the xml library you use.

我有Android开发的经验,但你使用android.sax包?那么我想 android.sax.StartElementListener 将是一个很好的人选。它接收属于一个特定的标签的属性。

I have no experience with android development, but do you use the android.sax package? Then i think android.sax.StartElementListener would be a good candidate. It receives the attributes that belong to a specific tag.

这篇关于从互联网上解析XML(yr.no)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆