PHP DOMDocument getElementsByTagname?

前端 未结 2 1700
耶瑟儿~
耶瑟儿~ 2020-12-17 18:21

This is driving me bonkers... I just want to add another img node.

$xml = <<


        
相关标签:
2条回答
  • 2020-12-17 18:57

    DOMDocument::getElementsByTagName doesn't return an array, it returns a DOMNodeList. You need to use the item method to access its items:

    $album = $xmlDoc->getElementsByTagname('album')->item(0);
    
    0 讨论(0)
  • 2020-12-17 19:01
    // Parse error: syntax error, unexpected '[' in /Applications/XAMPP/xamppfiles/htdocs/admin/tests/DOMDoc.php  on line 17
    

    you cant do this in php

    $album = $xmlDoc->getElementsByTagname('album')[0];
    

    you have to do this

    $albumList = $xmlDoc->getElementsByTagname('album');
    $album = $albumList[0];
    

    EDIT: getElementsByTagname returns an object so you can do this (above code is incorrect)...

    $album = $xmlDoc->getElementsByTagname('album')->item(0);
    

    This error....

    // Fatal error: Call to undefined method DOMNodeList::appendChild() in /Applications/XAMPP/xamppfiles/htdocs/admin/tests/DOMDoc.php on line 19
    

    DOMNodeList doesnt have an appendChild method. DOMNode does.

    0 讨论(0)
提交回复
热议问题