在 android studio 中使用 Jsoup 解析 xml 数据
Parsing xml data with Jsoup in android studio
我的以下代码似乎不起作用:
private fun xmlParse_Jsoup() {
thread {
val doc = Jsoup.parse("http://xmlweather.vedur.is/?op_w=xml&type=forec&lang=is&view=xml&ids=1;422")
val listItems: Elements = doc.select("ul.list > li")
for (item in listItems) System.out.println(item.text())
val strings = doc.getElementsByTag("forecast")
}
}
现在我只是尝试读取文件,但最终输出将是 下的所有标签、ftime、F、D、T 和 W 标签。
首先,select所有forecast
个元素:
val listItems: Elements = doc.select("forecast")
接下来,遍历列表并打印所需的 children:
for (item in listItems) {
System.out.println(item.select("ftime"));
System.out.println(item.select("f"));
System.out.println(item.select("d"));
System.out.println(item.select("t"));
System.out.println(item.select("w"));
}
如果只想打印 child 节点内包含的文本,请替换上面的语句:
System.out.println(item.select(/* ... */));
与:
System.out.println(item.select(/* ... */).text());
我的以下代码似乎不起作用:
private fun xmlParse_Jsoup() {
thread {
val doc = Jsoup.parse("http://xmlweather.vedur.is/?op_w=xml&type=forec&lang=is&view=xml&ids=1;422")
val listItems: Elements = doc.select("ul.list > li")
for (item in listItems) System.out.println(item.text())
val strings = doc.getElementsByTag("forecast")
}
}
现在我只是尝试读取文件,但最终输出将是 下的所有标签、ftime、F、D、T 和 W 标签。
首先,select所有forecast
个元素:
val listItems: Elements = doc.select("forecast")
接下来,遍历列表并打印所需的 children:
for (item in listItems) {
System.out.println(item.select("ftime"));
System.out.println(item.select("f"));
System.out.println(item.select("d"));
System.out.println(item.select("t"));
System.out.println(item.select("w"));
}
如果只想打印 child 节点内包含的文本,请替换上面的语句:
System.out.println(item.select(/* ... */));
与:
System.out.println(item.select(/* ... */).text());