FreshPro
FreshPro

Reputation: 873

Accessing Node element in xml using Xpath

I'm uploading xml file and accessing values in a php class. Everything is going smooth but when i try to access $middle_of_month value I have a debug error called

"Call to undefined method DOMNodeList::getElementsByTagName()"

Here's how xml looks like

...
<Fdr>
  <MiddleOfMonth>
      <Data Value="0" MonthNumber="1" />
      ...

I get the other tags correct I verified with debug.

$fdr             = $key->getElementsByTagName(tag_constants::TAG_FDR);
$middle_of_month = $fdr->getElementsByTagName(tag_constants::TAG_MIDDLE_OF_MONTH);

I have the error in $middle_of_month line. I debugged like 2 hours and still couldn't figure out what's wrong. Any help would be appreciated

Edit :

tag_constants::TAG_FDR -> Fdr
tag_constants::TAG_MIDDLE_OF_MONTH ->MiddleOfMonth

Edit 2 :

$middle_of_month = $fdr->item(0)->getElementsByTagName(tag_constants::TAG_MIDDLE_OF_MONTH); 

seems to solve the problem

Upvotes: 1

Views: 97

Answers (1)

Telic
Telic

Reputation: 357

getElementsByTagName() returns a NodeList, and as the error implies, NodeLists do not in turn have this method (only Elements and Documents do).

You will need to pick an item from $fdr to run getElementsByTagName() on; perhaps like:

$fdr->item(0)->getElementsByTagName(tag_constants::TAG_MIDDLE_OF_MONTH);

Upvotes: 2

Related Questions