2009-08-17 51 views
1

的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? 
    } 
} 

我怎麼能知道每一個匹配節點的標籤名稱?

回答

3
$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()可以是很有益的。

+0

謝謝,我試過var_dump(),只有一堆DOMDocument對象出現了我也嘗試過nodeValue,但它不是很完美。我在小時候找這個,謝謝! – 2009-08-17 16:34:05