[英]Can't parse XML (from web) using JSoup
I am trying to work with small XML
files sent from web and parse few attributes from them. 我正在尝试处理从Web发送的小型
XML
文件,并从中解析一些属性。 How would I approach this in JSoup
? 我将如何在
JSoup
解决这个JSoup
? I know it's not XML
Parser but HTML
one but it supports XML
too and I don't have to build any Handlers, BuildFactories and such as I would have to in DOM
, SAX
etc. 我知道它不是
XML
解析器,而是HTML
解析器,但它也支持XML
,我不必构建任何Handlers,BuildFactories等,而我必须在DOM
, SAX
等中构建。
Here is example xml: LINK I can't paste it here because it exits the code tag after every line - if someone can fix that I would be grateful. 这是xml示例: LINK我无法将其粘贴到此处,因为它会在每一行之后退出代码标签-如果有人可以修复,我将不胜感激。
And here is my piece of code:: 这是我的代码:
String xml = "http://www.omdbapi.com/?t=Private%20Ryan&y=&plot=short&r=xml";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
// want to select first occurrence of genre tag though there is only one it
// doesn't work without .first() - but it doesn't parse it
Element genreFromXml = doc.select("genre").first();
String genre = genreFromXml.text();
System.out.println(genre);
It results in NPE at: NPE结果为:
String genre = genreFromXml.text();
There are 2 issues in your code: 您的代码中有2个问题:
String
representation of an URL
while an XML
content is expected, you should rather use the method parse(InputStream in, String charsetName, String baseUri, Parser parser)
instead to parse your XML as an input stream. XML
内容的同时,您提供URL
的String
表示,您应该使用parse(InputStream in, String charsetName, String baseUri, Parser parser)
代替将XML解析为输入流。 genre
in your XML
, genre
is an attribute of the element movie
. XML
没有元素genre
, genre
是元素movie
的属性。 Here is how your code should look like: 代码如下所示:
String url = "http://www.omdbapi.com/?t=Private%20Ryan&y=&plot=short&r=xml";
// Parse the doc using an XML parser
Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", "", Parser.xmlParser());
// Select the first element "movie"
Element movieFromXml = doc.select("movie").first();
// Get its attribute "genre"
String genre = movieFromXml.attr("genre");
// Print the result
System.out.println(genre);
Output: 输出:
Drama, War
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.