gpt4 book ai didi

java - 从互联网解析 xml(年份)

转载 作者:行者123 更新时间:2023-12-01 19:07:35 28 4
gpt4 key购买 nike

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

我开发了一个Android应用程序,它需要解析来自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;

然后从我的 Activity 课:

@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 数据的问题是,每个最终子项不仅仅包含一个值,而是可以包含更多标签(例如 <waveDirection unit="degree" value="250"/> )。因此,当我更改元素以使用此元素时,结果会变成 25 个不同的 String s (如果你算一下,所有带有标签 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 xml).. 一定有一些我应该解决的错误,但主要步骤已经完成。谢谢大家的回答,特别是user306848给我指明了我使用的方向!

最佳答案

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

从这里查看一些教程,

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

关于java - 从互联网解析 xml(年份),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9499929/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com