2012-08-12 67 views
0

这是我想要做的。如何使用字符串访问对象的图层?

我读了一个XML文件,需要访问某些属性。问题是这个XML文件可能有4种配置之一。

我希望能够做的是一样的东西:

if(condition1){ 
    $title='PropertyParent->Child'; 
} 
elseif(condition2){ 
    $title='DifferentProperty->AnotherLayer->DifferentChild'; 
} 

$myTitle = $xml->$title; 

,并将它在字符串中访问对象的结构。有没有办法做到这一点?我应该使用变量吗?

感谢您的帮助。

回答

0

好,要做到这一点的唯一方法是使用自己的函数:

function return_nested($xml_file, $condition) 
{ 
    $cond_arr = explode("->", $condition); 
    $document = new DOMDocument(); 
    $document->load($xml_file); 
    foreach($cond_arr as $cond) 
    { 
     $document = $document->getElementsByTagName($cond)->item(0); //process each condition 
    } 
    return $document; //return element 
} 
相关问题