检查PHP链中是否存在XML链

时间:2012-03-29 14:50:19

标签: php xml chaining

所以我有以下XML链:

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

XML示例 在一些XML文件中,$ xml-> assessment-> outcome_processing-> outcome-> ...确实存在。但这里只存在$ xml->评估。

<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()单独检查每个节点,当然还有一个函数,否则我有很多检查要做...

这是我的功能,因为字符串' - &gt;'无法正常工作无法作为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++;
  }

有没有人有一些想法,因为我现在没有任何灵感:(

提前致谢

1 个答案:

答案 0 :(得分:1)

返回链引用的数据,如果不存在则返回NULL

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');