DOM文档和XPath的 - 每个节点
问题描述:
的HTML标签鉴于使用DOMDocument
下面的PHP代码:DOM文档和XPath的 - 每个节点
$inputs = $xpath->query('//input | //select | //textarea', $form);
if ($inputs->length > 0)
{
for ($j = 0; $j < $inputs->length; $j++)
{
$input = $inputs->item($j);
$input->getAttribute('name'); // Returns the Attribute
$input->getTag(); // How can I get the input, select or textarea tag?
}
}
我怎么能知道每一个匹配节点的标签名称?
答
$inputs = $xpath->query('//input | //select | //textarea', $form);
// no need for "if ($inputs->length > 0) - the for loop won't run if it is 0
for ($j = 0; $j < $inputs->length; $j++)
{
$input = $inputs->item($j);
echo $input->nodeName;
}
参见:http://www.php.net/manual/en/class.domnode.php#domnode.props.nodename
P.S:除了寻找到的文档,一个var_dump()
可以是很有益的。
谢谢,我试过var_dump(),只有一堆DOMDocument对象出现了我也尝试过nodeValue,但它不是很完美。我在小时候找这个,谢谢! – 2009-08-17 16:34:05