Toni Beverin
Toni Beverin

Reputation: 55

xml parsing with php error

I can't find anything useful on google nor on here. I have trouble with parsing my xml file.

    $xml = new DOMDocument();
    $xml->load('racunixml.xml');
    $searchNode = $xml->getElementsByTagName( "racun" ); 
    foreach( $searchNode as $searchNode ) 
    {   
       $xmlNaruc=$searchNode->getElementsByTagName('narucitelj');
       $valNaruc = $xmlNaruc->getAttribute('naziv'); 
       $valID = $searchNode->getAttribute('redbr'); 
       $valDate = $searchNode->getAttribute( "date" ); 
        $valPaci = $searchNode->getAttribute( "pacijent"); 
    }

Returns this error referring to line: $valNaruc = $xmlNaruc->getAttribute('naziv');

Fatal error: Call to undefined method DOMNodeList::getAttribute()

Upvotes: 0

Views: 1422

Answers (1)

Alexey
Alexey

Reputation: 3484

if you're calling ::getElementsByTagName() (pay attention at "s" in it - plural) it always returns DOMNodeList. And it hints you politely with the error message. And if we look at manual http://www.php.net/manual/en/class.domnodelist.php this class doesn't have ::getAttribute() method. But DOMNode has and what you need to do here is to loop through the results returned by getElementsByTagName() in a foreach:

foreach($searchNode->getElementsByTagName('narucitelj') as $xmlNaruc) {
    $valNaruc = $xmlNaruc->getAttribute('naziv'); 
    .....
}

Upvotes: 2

Related Questions