0
DOMを使用してhtmlファイルを自動的に翻訳するコードを作成しています.PまたはSPANなどのテキスト要素を取得できません。これは私のPHPコードです:DOMによってPおよびSPANが検出されない
<?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ファイルです:
[[email protected] www]$ cat t1.html
<html>
<body>
<p>hello world<br/>
<span>just text</span>
</p>
</body>
</html>
[[email protected] www]$
そして、私がouptutとして取得する理由です:
[[email protected] 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='
'
[[email protected] www]$
あなたが見ることができるように<p>
と<span>
タグはDOMによって検出されません。それはなぜですか、そして、DOMでこれらの要素を返すにはどうしたらいいですか?