【问题标题】:Parse xml from internet (yr.no)从互联网解析 xml (yr.no)
【发布时间】:2012-03-18 23:40:46
【问题描述】:

我在 xml 解析方面没有经验,所以也许我写的一些东西对某些人来说看起来很愚蠢,也许我的一些术语不太正确。请原谅。

我开发了一个安卓应用程序,它需要解析来自YR.no 的天气数据。该组织提供了一个 api,其中包含提供某些 xml 格式数据的方法。比如说我想从这个http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad解析xml数据

我开发了一个可以做一些 xml 解析的代码,它可以在这个 http://www.w3schools.com/xml/simple.xml 中正常工作(作为测试)。

定义在我的 BaseFeedParser 类中获取什么的主要代码行是:

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;

然后从我的活动课:

@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);
}

所以它返回并打印我想要的数据(“比利时华夫饼”等)

我最初想解析的 yr.no 数据的问题是每个最终子节点不只包含一个值,而是可以有更多标签(例如&lt;waveDirection unit="degree" value="250"/&gt;)。因此,当我将元素更改为使用这个元素时,它会以 25 个不同的 Strings 结尾(如果您计算它们是所有带有标签 waveDirection 的不同子代),但每个值都是空的(如 String a = "")。我没有收到任何错误,我只得到了 25 个空字符串的列表。我试图达到我的元素的方式是这样的:

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 = "http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad";,因为它找到了文档的根和 25 个元素。

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

    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 xmls 测试过)。我应该解决一些错误,但主要步骤已经完成。谢谢大家的回答,尤其是 user306848 指出我使用的方向!

【问题讨论】:

    标签: java android xml xmlhttprequest xml-parsing


    【解决方案1】:

    使用 Dom Parser.......这很容易......

    从这里查看一些教程,

    http://www.roseindia.net/xml/dom/

    【讨论】:

    • 这看起来很有希望thanx!我会对教程做一些研究并告诉你!
    【解决方案2】:

    我认为您的代码假定存在代表您寻求的值的文本节点。 yr.no 域中的 xml 文件不是这种情况。

    您需要弄清楚如何从您使用的 xml 库的标签中读取属性。

    我没有android开发经验,但是你使用android.sax包吗?然后我认为android.sax.StartElementListener 将是一个不错的候选人。它接收属于特定标签的属性。

    【讨论】:

      【解决方案3】:

      使用this example

      他正在使用本地存储的 XML 文件。如果您获得 XML Feed,请将代码更改为以下内容:

        URL url = new URL("ENTER XML LINK");
        InputStream stream = url.openStream();
        Document doc = docBuilder.parse(stream);
      

      【讨论】:

      • 感谢您的回答!但这显示了如何获得像我测试过的那样简单的 xml(我很容易用本教程的代码再次解析它,是的,它工作正常)但是我不能在我真正想从中解析的 xml 上使用它
      • 所以例如你想要 WaveDirection 和其他的单位和值,但是当你解析它时没有显示?
      • 使用我的原始代码它显示一个空字符串.. 直到现在我才能形成它(我形成它以显示第一个 xml ok 但无法为另一个设置孩子..)
      猜你喜欢
      • 1970-01-01
      • 2012-02-12
      • 2013-03-28
      • 1970-01-01
      • 2019-08-25
      • 2012-07-31
      • 1970-01-01
      • 2011-07-17
      相关资源
      最近更新 更多