简体   繁体   中英

DOMDocument, getNamedItem and nodeValue

I get Fatal error: Call to undefined method DOMElement::getNamedItem() and I'm not sure why. There's definitely something I dont understand with this DOMdocument thing. Here's my code:

// load content of db
$dom = new DOMDocument('1.0');
$dom->load($file);

// make domnodelist with girls
$girls = $dom->getElementsByTagName('girl');

foreach($girls as $girl)
{
    $username = $girl->getNamedItem('username');
    echo $username->nodeValue;
}

Here's my xml:

<root>
  <girl username="xxxx" id="2012111003051009">
    <url>xxxx</url>
    <replied>false</replied>
    <thumbnail>dbs/db-thumbnails/xxxx.jpeg</thumbnail>
    <blacklisted>false</blacklisted>
    </girl>
</root>

username is an attribute of the DOMElement $girl. So what you need to use is getAttribute .

foreach($girls as $girl)
{
    $username = $girl->getAttribute('username');
    // or if using getNamedItem 
    // $username = $girl->attributes->getNamedItem ('username')->nodeValue
    echo $username;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM