1

I have this following XML

<Logs>
  <UnplugDate>
    <Date>2013-09-10T09:20:00</Date>
    <Date>2013-09-09T16:03:00</Date>
  </UnplugDate>

What I'm trying to do here is to read the values of both variables under <UnplugDate> tag.

I try to use the hasChildNodes() but when I debug It doesn't go inside

foreach($unplug_date as $node) block.

Any Idea how can I read these values? Thanks in advance

$logs    = $key->getElementsByTagName(tag_constants::TAG_LOGS);
$unplug_date = $logs->item(0)->getElementsByTagName(tag_constants::TAG_UNPLUG_DATE)->item(0);
foreach($unplug_date as $node) {
    if($node->hasChildNodes()) {
         foreach ($node->childNodes as $unplug_date_value) {
              $unplug_date_value = $unplug_date->getElementsByTagName(tag_constants::TAG_DATE)->item(0)->nodeValue;
         }
    }
}

NOTE:

tag_constants::TAG_LOGS        -> Logs
tag_constants::TAG_UNPLUG_DATE -> UnplugDate
tag_constants::TAG_DATE        -> Date
0

1 Answer 1

3

I've finally find the solution. Writing :

$test = $unplug_date->getElementsByTagName(tag_constants::TAG_DATE);

instead of

$unplug_date = $logs->item(0)->getElementsByTagName(tag_constants::TAG_UNPLUG_DATE)->item(0); 

solves the problem.

Sign up to request clarification or add additional context in comments.

4 Comments

But do you understand why? You were iterating over a DOMNode instead of DOMNodeList. I think using Xpath makes more sense here. You could evaluate this string(//Logs/UnplugDate/Date) to get that text
Yes, I understood that I was trying to get DOMNode. By the way I tried Xpath like //UnplugDate[@Date] but didn't work. Going to check your string as well
That xpath didn't work because @ denotes an attribute, you most likely meant: //UnplugDate/Date.
Oh I see now, I by using @ I'm trying to access an attribute which isn't even exists. Thanks guys for the tips

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.