java - 如何遍历这个XML来获取DATA?

标签 java xml traversal dom-traversal

我正在尝试获取 xml 中显示的项目的信息,如下所示:

<item>
  <title>The Colbert Report - Confused by Rick Parry With an "A" for America</title>

  <guid isPermaLink="false">http://www.hulu.com/watch/267788/the-colbert-report-confused-by-rick-parry-with-an-a-for-america#http%3A%2F%2Fwww.hulu.com%2Ffeed%2Fpopular%2Fvideos%2Fthis_week%3Frd%3D0</guid>
  <link>http://rss.hulu.com/~r/HuluPopularVideosThisWeek/~3/6aeJ5cWMBzw/the-colbert-report-confused-by-rick-parry-with-an-a-for-america</link>
  <description>&lt;a href="http://www.hulu.com/watch/267788/the-colbert-report-confused-by-rick-parry-with-an-a-for-america#http%3A%2F%2Fwww.hulu.com%2Ffeed%2Fpopular%2Fvideos%2Fthis_week%3Frd%3D0"&gt;&lt;img src="http://thumbnails.hulu.com/507/40025507/40025507_145x80_generated.jpg" align="right" hspace="10" vspace="10" width="145" height="80" border="0" /&gt;&lt;/a&gt;&lt;p&gt;The fat cat media elites in Des Moines think they can sit in their ivory corn silos and play puppet master with national politics.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.hulu.com/users/add_to_playlist?from=feed&amp;video_id=267788"&gt;Add this to your queue&lt;/a&gt;&lt;br/&gt;Added: Fri Aug 12 09:59:14 UTC 2011&lt;br/&gt;Air date: Thu Aug 11 00:00:00 UTC 2011&lt;br/&gt;Duration: 05:39&lt;br/&gt;Rating: 4.7 / 5.0&lt;br/&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/HuluPopularVideosThisWeek/~4/6aeJ5cWMBzw" height="1" width="1"/&gt;</description>

  <pubDate>Fri, 12 Aug 2011 09:59:14 -0000</pubDate>
  <media:thumbnail height="80" width="145" url="http://thumbnails.hulu.com/507/40025507/40025507_145x80_generated.jpg" />
  <media:credit>Comedy Central</media:credit>
  <dcterms:valid>start=2011-08-12T00:15:00Z; end=2011-09-09T23:45:00Z; scheme=W3C-DTF</dcterms:valid>
  <feedburner:origLink>http://www.hulu.com/watch/267788/the-colbert-report-confused-by-rick-parry-with-an-a-for-america#http%3A%2F%2Fwww.hulu.com%2Ffeed%2Fpopular%2Fvideos%2Fthis_week%3Frd%3D0</feedburner:origLink></item>
<item>

我需要标题、链接、媒体:缩略图网址和说明。

我使用了以下方法:http://www.rgagnon.com/javadetails/java-0573.html

标题和链接工作正常,但图像网址和描述则不然。

有人可以帮我解决这个问题吗?

最佳答案

您可以使用 XPath 从 XML 文档中检索特定数据。

例如为了检索 url 属性的内容:

XPathFactory 工厂 = XPathFactory.newInstance();

XPath xpath = factory.newXPath();
String url = xpath.evaluate("/item/media:thumbnail/@url", new InputSource("data.xml"));

关于java - 如何遍历这个XML来获取DATA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7210869/

相关文章:

java - 使用 ScrollPane java 的动态按钮

Mac 上 Java 编译错误

java - 设计应用程序模型API

xml - 如何将 xml 片段合并到 tomcat 的 server.xml 中

json - 解析和转换的区别?

c++:将函数作为参数传递给另一个函数

java - 从 Java 字符串中去除所有不可打印字符的最快方法

javascript - jQuery 从一组(组)类中选择 div

graph - 无需重新访问即可遍历 3D 坐标的算法

c# - 在 C# 中将字符串值格式化为 $xx.xx 格式