0

これが私がやろうとしていることです。

XML ファイルを読み取り、特定のプロパティにアクセスする必要があります。問題は、この XML ファイルに 4 つの構成のいずれかが含まれている可能性があることです。

私ができるようにしたいのは、次のようなものです。

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

$myTitle = $xml->$title;

文字列内のオブジェクト構造にアクセスします。これを行う方法はありますか?可変変数を使用する必要がありますか?

ご協力いただきありがとうございます。

4

1 に答える 1

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
}
于 2012-08-12T02:33:50.313 に答える