使用php简单HTML DOM解析器



我该如何捕捉,只捕捉动物的名字。

我的zoo.xml

<?xml version="1.0" encoding="UTF-8"?>
<zoo>
<animal id="315" name="Lion"></animal>
<animal id="316" name="Cat"></animal>
</zoo>

我做了这样的测试,但结果是空的。

编辑。

$html = file_get_html('zoo.xml');
foreach($html->find('animal') as $animal) {
$item['name'] = $animal->find('name',0 )->plaintext;
$animals[] = $item;
}
print_r($animals);

结果

Array
(
[0] => Array
(
[name] => 
)
[1] => Array
(
[name] => 
)
)

我的做法正确吗?

动物名称在属性中,可以使用getAttribute:

$html = file_get_html('zoo.xml');
foreach($html->find('animal') as $animal) {
$animals[] = ['name' => $animal->getAttribute('name')];
}
print_r($animals);

结果:

Array
(
[0] => Array
(
[name] => Lion
)
[1] => Array
(
[name] => Cat
)
)

在@KarstenKoop e的帮助下,Thequathbird获得了它。

$html = file_get_html('zoon.xml');
foreach($html->find('animal') as $animal) {
$item['name'] = $animal->name;
$animals[] = $item;
}
print_r($animals);

感谢

最新更新