dth96108 2018-11-06 13:51
浏览 333
已采纳

使用php Simple HTML DOM Parser

how do I capture, just the names of the animals.

my zoo.xml

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

I did the test like this, but it returns empty.

edit.

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

result

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

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

)

Am I doing the right way?

  • 写回答

2条回答 默认 最新

  • doujing5150 2018-11-06 14:24
    关注

    The animal names are in the attributes, you can use getAttribute:

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

    Result:

    Array
    (
        [0] => Array
            (
                [name] => Lion
            )
    
        [1] => Array
            (
                [name] => Cat
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douwen2158 2018-11-06 15:21
    关注

    With the help of @KarstenKoop e @Thefourthbird got it.

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

    Thanks

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何利用C语言实现用最小二乘法选配两个经验公式
  • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?
  • ¥15 有人会思科模拟器嘛?
  • ¥30 遇到一个的问题,请教各位
  • ¥20 matlab报错,vflux计算潜流通量
  • ¥15 我该如何实现鼠标按下GUI按钮时就执行按钮里面的操作的方法
  • ¥15 关于#硬件工程#的问题:我这边有个锁相环电路没有效果
  • ¥15 20款 27寸imac苹果一体机装win10后,蓝牙耳机和音响放歌曲卡顿断断续续.
  • ¥15 VB.NET 父窗体调取子窗体报错
  • ¥15 python海龟作图如何改代码使其最后画出来的是一个镜像翻转的图形