xpath表达式在html文件中查找元素的所有occerence [英] xpath expression to find all occerence of a element in html file

查看:124
本文介绍了xpath表达式在html文件中查找元素的所有occerence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html文件,我需要找到< object> 标签的出现次数。在Java中,我使用XPathExpression //:object [contains(@type,video /)或contains(@type,audio /)] 来查找< object> 标签的出现。它正在检测< object> 标记,但我希望我的文件中存在对象标记的数量。

I have an html file where I need to find the number of occurrences of the <object> tag. In Java I'm using the XPathExpression //:object[contains(@type,"video/") or contains(@type,"audio/")] to find the occurences of the <object> tag. It's detecting the <object> tag but I want the count of number of object tags present in my file.

result = (Boolean) expression.evaluate(inputSource, XPathConstants.BOOLEAN);

其中inputSource包含我的html文件。它返回一个布尔值,当我尝试同样的事情与
xpathValue =(Double)expression.evaluate(inputSource,XPathConstants.NUMBER); 获取结果为NaN。

where inputSource contains my html file. It is returning a boolean value, when I tried the same thing with xpathValue = (Double) expression.evaluate(inputSource, XPathConstants.NUMBER); getting result as NaN.

解决方案是什么?

推荐答案

我的Java和Xpath有点模糊,但是如果您评估

I'm a bit fuzzy on my Java and Xpath, but if you evaluate

expression.evaluate(inputSource,XPathConstants.NODELIST)

您应该找回与您的查询匹配的所有节点的NodeList,并且您可以获取该列表的长度。

You should get back a NodeList of all the Nodes matching your query, and you can just get the length of that list.

这篇关于xpath表达式在html文件中查找元素的所有occerence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆