2010-03-17 51 views
2

$element->children()我不能得到当前元素的所有子元素。在PHP中使用simplexml获取父元素?

但我怎样才能获得当前元素的所有父元素与simplexml?

可以说,它看起来像这样:

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

如果我当前$elementid=3,我想在Mac和苹果实体ID。

这可能与simplexml因为我找不到任何函数允许我这样做?

+1

http://stackoverflow.com/questions/2174263/access-an-elements-parent-with-phps-simplexml – 2010-03-17 21:51:29

回答

5
$element->xpath("ancestor::entity/@id") 
+1

的重复,你能解释一下 “祖先::实体/ @ ID” 给我吗? – 2010-03-18 00:01:18

+0

它选择所有祖先''节点的'id'属性。学习XPath,这真的不难。 ;) – Tomalak 2010-03-18 07:35:54