Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get parent elements in xml with simplexml in php?

Tags:

php

xml

simplexml

with $element->children() i cant get all the child elements of current element.

but how can i get all the parent elements for current element with simplexml?

lets say it looks like this:

<entity id=1>
   <name>apple</name>
   <entities>
      <entity id=2>
         <name>mac</name>
         <entities>
            <entity id=3>
               <name>safari</name>
            </entity>
         </entities>
      </entity>
   </entities>
</entity>

if my current $element is id=3, i want to get the mac and apple entity ids.

is this possible with simplexml cause i cant find any function allowing me to do this?

like image 361
never_had_a_name Avatar asked Sep 16 '26 13:09

never_had_a_name


1 Answers

 $element->xpath("ancestor::entity/@id")
like image 83
Tomalak Avatar answered Sep 18 '26 05:09

Tomalak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!