2012-03-29 8 views
0

に存在するのであれば、私は次のXMLチェーンを持っている:チェックXMLチェーンは、PHP

$xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue 

XMLの例 いくつかのXML-ファイルで$ XML-> assessment-> outcomes_processing-> outcomes-> .. 。 ある。しかし、ここでは$ xml-> assessmentのみが存在します。

<questestinterop> 
    <assessment ident="nedolat78356769204914" title="welkom woordenschat 4"> 
    <qtimetadata> 
     <qtimetadatafield> 
     <fieldlabel>qmd_assessmenttype</fieldlabel> 
     <fieldentry>Assessment</fieldentry> 
     </qtimetadatafield> 
    </qtimetadata> 
    <section ident="nedolat78356769204915" title="Woordenschat"> 
     <selection_ordering> 
     <selection/> 
     <order order_type="Sequential"/> 
     </selection_ordering> 
     <item ident="QTIEDIT:FIB:34932158" title="Oefening 1"> 
     ... 
     </item> 
    </section> 
    </assessment> 
</questestinterop> 

現時点では私はエラーが発生します:「非オブジェクトのプロパティを取得しようとします」。 のノードがの結果ノードが存在しないため、どちらが論理です。

私はisset()で解決策を試しましたが、これは明らかに動作しません。

isset($xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue) ? ... : null 

これは私に同じエラーを与えます。ノードの結果はではないので、すぐにエラーがスローされます。その解決法は、isset()を使って個々のノードを個別にチェックすることができます。そうでなければ関数を使ってチェックすることができます。

これは私の関数です。 - >」PHPコードとして処理することができませんでした。

// xml: xml structure form SimpleXML 
// chain: assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue 
function checkIfXMLChainExists($xml, $chain) { 
    $xmlChain = ''; 
    $i = 0; 
    $nodes = explode('->', $chain); 
    $numItems = count($nodes); 

    // experimenting with eval() to treat '->' as ->; failed to work 
    $a = '$xml->assessment->outcomes_processing'; 
    $t = eval($a); 
    echo $t; 
    print_r($t); 

    foreach ($nodes as $node) { 
    $xmlChain .= '->' . $node; 

    if (isset($xml->$xmlChain)) { // Node exists 
     if ($i+1 == $numItems) { 
     return $xml->$xmlChain; 
     } 
    } else { // Node does not exists 
     return 'does not exist'; 
    } 
    $i++; 
    } 

は私が

+0

xpathの使い方は? :P – pleasedontbelong

+0

あなたのXMLによって異なります。サンプルを見ることはできますか?その構造に応じて、例えば '$ xml-> assessment [0] - > outcomes_processing'のようなことをしなければならないかもしれません。 – halfer

+0

'eval'、btwを使用するのではなく、' $ xml-> children() 'を反復する方が良いでしょう。 – halfer

答えて

1

がデータを返した瞬間:(事前に

おかげで、どんなインスピレーションを持っていないので、誰もがいくつかのアイデアを持っていチェーンによって参照される、またはもし存在しなければ10。

function getDataIfExists() { 

    // We accept an unknown number of arguments 
    $args = func_get_args(); 

    if (!count($args)) { 
    trigger_error('getDataIfExists() expects a minimum of 1 argument', E_USER_WARNING); 
    return NULL; 
    } 

    // The object we are working with 
    $baseObj = array_shift($args); 

    // Check it actually is an object 
    if (!is_object($baseObj)) { 
    trigger_error('getDataIfExists(): first argument must be an object', E_USER_WARNING); 
    return NULL; 
    } 

    // Loop subsequent arguments, check they are valid and get their value(s) 
    foreach ($args as $arg) { 
    $arg = (string) $arg; 
    if (substr($arg, -2) == '()') { // method 
     $arg = substr($arg, 0, -2); 
     if (!method_exists($baseObj, $arg)) return NULL; 
     $baseObj = $baseObj->$arg(); 
    } else { // property 
     if (!isset($baseObj->$arg)) return NULL; 
     $baseObj = $baseObj->$arg; 
    } 
    } 

    // If we get here $baseObj will contain the item referenced by the supplied chain 
    return $baseObj; 

} 

// Call it like this: 
$subObj = getDataIfExists($xml, 'assessment', 'outcomes_processing', 'outcomes', 'decvar', 'attributes()', 'cutvalue'); 
+0

うわーは実際にはとてもシンプルですが、私は決してそれに来なかったでしょう。ありがとう! –