我正在编写一段代码,用DOM自动翻译html文件,但我无法获得像p或SPAN这样的文本元素。这是我的PHP代码:
<?php
$fname="t1.html";
parse_file($fname);
function process_element($elt) {
echo "name=".$elt->nodeName." type=".$elt->nodeType."'n";
echo "val='".$elt->nodeValue."''n";
echo "text_content='".$elt->textContent."''n";
if (!isset($item->childNodes)) return;
$children = $item->childNodes;
foreach($children as $child) {
process_element($child);
}
}
function parse_file($filename) {
$document=new DOMDocument();
$document->load($filename);
$items=$document->documentElement;
foreach($items->childNodes as $item) {
process_element($item);
}
}
?>
这是我正在使用的示例HTML文件:
[niko@dev1 www]$ cat t1.html
<html>
<body>
<p>hello world<br/>
<span>just text</span>
</p>
</body>
</html>
[niko@dev1 www]$
这就是为什么我得到了这样的答案:
[niko@dev1 www]$ php -f p.php
name=#text type=3
val='
'
text_content='
'
name=body type=1
val='
hello world
just text
'
text_content='
hello world
just text
'
name=#text type=3
val='
'
text_content='
'
[niko@dev1 www]$
如您所见,DOM不会检测到<p>
和<span>
标记。为什么会这样?我如何使DOM返回这些元素?
process_element
方法的后半部分突然使用了未声明的变量$item
而不是$elmt
。更正:
function process_element($elt) {
echo "name=".$elt->nodeName." type=".$elt->nodeType."'n";
echo "val='".$elt->nodeValue."''n";
echo "text_content='".$elt->textContent."''n";
if (!isset($elt->childNodes)) return;
$children = $elt->childNodes;
foreach($children as $child) {
process_element($child);
}
}