gpt4 book ai didi

java - XML 和 Dom4J。如何使用迭代器检索值?

转载 作者:行者123 更新时间:2023-11-30 04:14:52 24 4
gpt4 key购买 nike

我正在迭代 this webpage 处的所有数据(下面的示例 xml)我对如何获取所需的值感到困惑。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/i/xml/xsl_formatting_rss.xml"?>
<rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" version="2.0">
<channel>
<title>Ariana Resources News</title>
<link>http://www.iii.co.uk/investment/detail?code=cotn:AAU.L&amp;display=news</link>
<description />
<item>
<title>Ariana Resources PLC - Environmental Impact Assessment Submitted for Kiziltepe</title>
<link>http://www.iii.co.uk/investment/detail?code=cotn:AAU.L&amp;display=news&amp;action=article&amp;articleid=9084833&amp;from=rss</link>
<description>Some Article information</description>
<pubDate>Fri, 30 Aug 2013 07:00:00 GMT</pubDate>
</item>
<item>
<title>Ariana Resources PLC - Directors' Dealings and Holding in Company</title>
<link>http://www.iii.co.uk/investment/detail?code=cotn:AAU.L&amp;display=news&amp;action=article&amp;articleid=9053338&amp;from=rss</link>
<description>Some Article information</description>
<pubDate>Wed, 31 Jul 2013 07:00:00 GMT</pubDate>
</item>
<item>
<title>Ariana Resources PLC - Directorship Changes</title>
<link>http://www.iii.co.uk/investment/detail?code=cotn:AAU.L&amp;display=news&amp;action=article&amp;articleid=9046582&amp;from=rss</link>
<description>Some Article information</description>
<pubDate>Wed, 24 Jul 2013 09:31:00 GMT</pubDate>
</item>
<item>
<title>Ariana Resources PLC - Ariana Resources plc : Capital Reorganisation</title>
<link>http://www.iii.co.uk/investment/detail?code=cotn:AAU.L&amp;display=news&amp;action=article&amp;articleid=9038706&amp;from=rss</link>
<description>Some Article information</description>
<pubDate>Wed, 24 Jul 2013 09:31:00 GMT</pubDate>
</item>
<item>
</channel>
</rss>

我查看了 dom4j快速入门指南,尽管我怀疑我还不太明白。

我怎样才能以这样的方式进行迭代:

  1. 检查每一个,如果它有今天的日期并且......
  2. 获取每个特定的值,并且

此时我得到了以下内容,我认为第二个循环是非常错误的......非常感谢任何帮助:

    //Create a null Document Object
Document theXML = null;

//Get the document of the XML and assign to Document object
theXML = parseXML(url);

//Place the root element of theXML into a variable
Element root = theXML.getRootElement();


// iterate through child elements of root
for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
// do something

// iterate through child elements of root with element name "item"
for ( Iterator j = root.elementIterator( "item" ); j.hasNext(); ) {
Element foo = (Element) j.next();

String rnsHeadline = "";
String rnsLink = "";
String rnsFullText = "";
String rnsConstituentName = "";



Rns rns = new Rns(null, null, null, null);

}

最佳答案

使用 dom4j 的 XPath 功能:

// Place the root element of theXML into a variable
List<? extends Node> items =
(List<? extends Node>)theXML.selectNodes("//rss/channel/item");

// RFC-dictated date format used with RSS
DateFormat dateFormatterRssPubDate =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);

// today started at this time
DateTime timeTodayStartedAt = new DateTime().withTimeAtStartOfDay();

for (Node node: items) {
String pubDate = node.valueOf( "pubDate" );
DateTime date = new DateTime(dateFormatterRssPubDate.parse(pubDate));
if (date.isAfter(timeTodayStartedAt)) {
// it's today, do something!
System.out.println("Today: " + date);
} else {
System.out.println("Not today: " + date);
}
}

Dom4j 需要 jaxen XPath 工作的依赖关系。我用过JodaTime比较日期,因为它比使用 java 内置日期干净得多。 Here's the full example .

请注意,dom4j 并未真正得到维护,因此您可能也对 this discussion about dom4j alternatives 感兴趣。 .

关于java - XML 和 Dom4J。如何使用迭代器检索值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18673628/

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