- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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给我指明了我使用的方向!
最佳答案
关于java - 从互联网解析 xml(年份),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9499929/
我正在完成一项让我难堪好几天的学校作业。任务是使用 View (VAvailableGolfers) 将与从组合框中选择的给定事件/年份无关的高尔夫球手填充到列表框中。以下是表中的数据: 那么,表单上
给定一年和那年的某一天,我如何获得完整的日期?例如:60/2014 = 2014 年 3 月 1 日 和 61/2016 = 2016 年 3 月 1 日 注释:-年和日可以作为单独的参数传递。 -结
我正在使用 ExtJS 3,当我想为日期选择器选择日期时,我只能设置日期和月份(通过左/右箭头)。月/年面板会放下来,但很快就会消失,因此无法选择它。这是最能描述该行为的桌面记录:http://fli
我正在尝试获取用于在 div 中显示当前月份和年份的 jquery 或 java 脚本代码,但目前还不能。我的意思是我想以这种格式显示当前的月份和年份:October 2012 这样每个月我都不需要编
我有这段代码可以从数据库中获取一个字段: $end_date=$row1['end_date']; 如果我打印它,它会给我这样的信息:25-09-2012我需要的是获取月份值、年份和日期。类似于: $
我有两个格式如下的数据集: df1 #> Artist Album Year #> 1 Beatles Sgt. Pepper's 1967 #>
我使用 jQuery 验证插件和 maskedInput 插件对输入进行了自定义日期和时间验证。 有没有办法在自定义验证中进行验证以防止输入大于当前年份的年份? 我的代码: $("#date").ma
我在 xml 解析方面没有经验,所以也许我写的一些东西对某些人来说看起来很愚蠢,也许我的一些术语不太正确。请原谅。 我开发了一个Android应用程序,它需要解析来自YR.no的天气数据。 。该组织提
我需要当前年份,月份和日期为3个不同的变量。下面的代码给出了日期时间 val now = Calendar.getInstance().getTime() 2016年9月29日星期四18:27:38
在模态对话框中使用日期选择器,请不要在 Firefox 19.0.2 中使用更改月/年下拉列表,请参阅: http://jsfiddle.net/469zV/2/ HTML
我希望日期输入在年份有 4 位数字后停止输入。我尝试使用 HTML 中的 min 和 max 组件,但它仍然允许您键入删除第一个数字。参见示例 1。 请参阅示例 2 以了解可以输入超过 4 位数字的年
因此不同是基于唯一的月/年,而不仅仅是一个不同的月份(所以我希望 2011 年 1 月和 2012 年 1 月是不同的) // Test set of data List Compl
在 Programming in the Key of C# 中,作者给出了一个示例(附源代码),说明如何将日期(年、月、日 -- 数字)打包为 32 位整数。在示例中,作者将信息打包如下: int
这里我有一个任务,如果我写这样的代码,年份应该像 1947 年到 2016 年一样绑定(bind)在 DropdownList 中 1947 2016 拍了一整天 最佳答案 例如,
显然,我的 SQL 不是最好的,但我想做的是通过查找条目中的最大年份和月份来获取数据库中的最新日期。现在我有: select max(Month), max(Year) from posts wher
我试图弄清楚是否有任何 ID 发生在任何早年(即 dfo 中的 Duplicate 列)。如果是这样,我想将该行标记为重复行并包括 ID 首次出现的年份(即 Year_Duplicate)。 我确实有
我已经从 df 中提取了列列表,这些列是字符串类型,如下所示:1999-1(如 1999 年第一个月)。 我想删除 2000 年之前的所有列,因此我提取了列列表并使用列表理解来检查字符串的第一个字符是
日期时间是什么时候?我的 DateTimePicker 控件的源更改为 null,我想选择另一个日期,日历下拉列表默认为最后选择的月份和年份。 我正在为 DOB 使用 DateTimePicker,因
我正在获取系统当前日期并尝试在 TextView 中显示它。 尝试下面的代码后 private OnClickListener listener1 = new OnClickListener() {
我想使用 knockoutJS 库验证年份。我面临的问题是,当用户输入错误的年份时,不会对年份进行验证。以下是年份错误的情况。 (01/01/12 becomes 01/01/0012) (01/0
我是一名优秀的程序员,十分优秀!