gpt4 book ai didi

java - 无法从 XML 中提取数据

转载 作者:行者123 更新时间:2023-12-01 14:40:41 25 4
gpt4 key购买 nike

我使用 getElementBytag 方法从以下 XML 文档中提取数据(Yahoo 财经新闻 api http://finance.yahoo.com/rss/topfinstories )

<小时/>

我使用以下代码。它使用 getelementsBytag 方法获取新项目和标题没有问题,但由于某种原因,在按标签搜索时不会拾取链接。它只选取链接元素的结束标记。是XML文档的问题还是jsoup的问题?

import java.io.IOException;         
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

class GetNewsXML {
/**
* @param args
*/
/**
* @param args
*/
public static void main(String args[]){
Document doc = null;
String con = "http://finance.yahoo.com/rss/topfinstories";
try {
doc = Jsoup.connect(con).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements collection = doc.getElementsByTag("item");// Gets each news item
for (Element c: collection){
System.out.println(c.getElementsByTag("title"));
}
for (Element c: collection){
System.out.println(c.getElementsByTag("link"));
}
}

最佳答案

你得到<link /> http://... ;该链接位于 link之后 -标记为文本节点。

但这不是问题:

final String url = "http://finance.yahoo.com/rss/topfinstories";

Document doc = Jsoup.connect(url).get();


for( Element item : doc.select("item") )
{
final String title = item.select("title").first().text();
final String description = item.select("description").first().text();
final String link = item.select("link").first().nextSibling().toString();

System.out.println(title);
System.out.println(description);
System.out.println(link);
System.out.println("");
}

说明:

item.select("link")  // Select the 'link' element of the item
.first() // Retrieve the first Element found (since there's only one)
.nextSibling() // Get the next Sibling after the one found; its the TextNode with the real URL
.toString() // Get it as a String

通过您的链接,此示例将打印所有元素,如下所示:

Tax Day Freebies and Deals
You made it through tax season. Reward yourself by taking advantage of some special deals on April 15.
http://us.rd.yahoo.com/finance/news/rss/story/SIG=14eetvku9/*http%3A//us.rd.yahoo.com/finance/news/topfinstories/SIG=12btdp321/*http%3A//finance.yahoo.com/news/tax-day-freebies-and-deals-133544366.html?l=1

(...)

关于java - 无法从 XML 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15991511/

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