0

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()

1
  • You are not defining xmlNaruc anywhere? Commented Feb 25, 2013 at 20:17

1 Answer 1

2

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'); 
    .....
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.