XPath表达式根据兄弟的值返回值

时间:2010-02-12 19:30:31

标签: xml xpath

以下是我尝试从中提取数据的示例xml文件:

<rss>
 <channel>
  <item>
   <title>First Title</title>
   <description>description text</description>
   <component>Distribution - Maintain</component>
   <timeoriginalestimate seconds="3600">1 hour</timeoriginalestimate>
   <timespent seconds="1860">31 minutes</timespent>
  </item>
  <item>
   <title>Second Title</title>
   <description>description text</description>
   <component>Reporting - Security</component>
   <timeoriginalestimate seconds="3600">1 hour</timeoriginalestimate>
   <timespent seconds="1860">31 minutes</timespent>
  </item>
  <item>
   <title>third Title</title>
   <description>description text</description>
   <timeoriginalestimate seconds="5400">1 hour, 30 Minutes</timeoriginalestimate>
   <timespent seconds="2700">45 minutes</timespent>
  </item>
  <item>
   <title>Fourth Title</title>
   <description>description text</description>
   <component>Usability</component>
  </item>
  <item>
   <title>Fifth Title</title>
   <description>description text</description>
   <component>Distribution - Maintain</component>
   <timeoriginalestimate seconds="3600">1 hour</timeoriginalestimate>
   <timespent seconds="7200">2 hours</timespent>
  </item>
 </channel>
</rss>

我希望按timeoriginalestimate值收集timespentcomponent个节点。我想将component的值存储到地图或哈希中作为键,然后将值作为timeoriginalestimatetimespent的总差异

  

K =“分布 - 维持”V = 2小时-2小时31分钟

部分item不会有component,而某些component则没有时间值。在这种情况下,我希望将时间值添加到“其他”组件的运行总计中。

我在java中写这个,我打算打印一份报告,显示估计了多少时间与每个组件实际花费的时间。我不知道该怎么做。

非常感谢任何帮助。谢谢!

计算值出现次数的示例代码:

private XPath featureXPath = XPath.newInstance("count(//rss/channel/item/type[text()='New Feature'])");
LinkedHashMap<String, Double> metrics = new LinkedHashMap<String, Double>();
metrics.put("New Features", (Double)featureXPath.selectSingleNode(doc));

我只是不确定如何将时间值添加到未知密钥并仅为其各自的密钥添加时间值

1 个答案:

答案 0 :(得分:0)

我想在这里提出两个问题 - (1)如何使用XPath从文档中获取字符串和数字,以及(2)如何处理缺少数据的案例。我不清楚你想要做什么(2)缺少数据。您的示例包含一个item,它根本没有时间信息,甚至没有timespent - 我不知道您希望如何处理它。

下面的代码处理提取和数学(1)。在XPath中进行减法允许我进行2次XPath调用而不是3次。我使用立即评估来保持XPath表达式接近我使用它们的位置 - 这对我来说似乎更清楚。您可能希望将它们从循环中拉出并使用xpath.compile()进行编译,以避免在每次迭代时重新编译它们,如果您的分析显示了显着的节省。

XPath xpath = XPathFactory.newInstance().newXPath();
// find all <item>s, regardless of missing data
NodeList items = (NodeList)xpath.evaluate("/rss/channel/item",
                                          doc,
                                          XPathConstants.NODESET);
for (int i=0; i<items.getLength(); i++) {
    Node item = items.item(i);
    // this evaluates to true when all three child elements are present
    // adjust to suit your needs
    if ((Boolean)xpath.evaluate
        ("component and timeoriginalestimate and timespent",
         item, XPathConstants.BOOLEAN)) {
        // handle the "normal" case
        String component = (String)xpath.evaluate
            ("component", item, XPathConstants.STRING);
        Double time = (Double)xpath.evaluate
            ("timeoriginalestimate/@seconds - timespent/@seconds",
             item, XPathConstants.NUMBER);
        map.put(component, time);
    } else {
        // handle the "other" case
    }
}
相关问题