Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: None  |  size: 2.61 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Check if XML Chain Exists in PHP
  2. $xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue
  3.        
  4. <questestinterop>
  5.   <assessment ident="nedolat78356769204914" title="welkom woordenschat 4">
  6.     <qtimetadata>
  7.       <qtimetadatafield>
  8.         <fieldlabel>qmd_assessmenttype</fieldlabel>
  9.         <fieldentry>Assessment</fieldentry>
  10.       </qtimetadatafield>
  11.     </qtimetadata>
  12.     <section ident="nedolat78356769204915" title="Woordenschat">
  13.       <selection_ordering>
  14.         <selection/>
  15.         <order order_type="Sequential"/>
  16.       </selection_ordering>
  17.       <item ident="QTIEDIT:FIB:34932158" title="Oefening 1">
  18.       ...
  19.       </item>
  20.     </section>
  21.   </assessment>
  22. </questestinterop>
  23.        
  24. isset($xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue) ? ... : null
  25.        
  26. // xml: xml structure form SimpleXML
  27. // chain: assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue
  28. function checkIfXMLChainExists($xml, $chain) {
  29.   $xmlChain = '';
  30.   $i = 0;
  31.   $nodes = explode('->', $chain);
  32.   $numItems = count($nodes);
  33.  
  34.   // experimenting with eval() to treat '->' as ->; failed to work
  35.   $a = '$xml->assessment->outcomes_processing';
  36.   $t = eval($a);
  37.   echo $t;
  38.   print_r($t);
  39.  
  40.   foreach ($nodes as $node) {
  41.     $xmlChain .= '->' . $node;
  42.  
  43.     if (isset($xml->$xmlChain)) { // Node exists
  44.       if ($i+1 == $numItems) {
  45.         return $xml->$xmlChain;
  46.       }
  47.     } else { // Node does not exists
  48.       return 'does not exist';
  49.     }
  50.     $i++;
  51.   }
  52.        
  53. function getDataIfExists () {
  54.  
  55.   // We accept an unknown number of arguments
  56.   $args = func_get_args();
  57.  
  58.   if (!count($args)) {
  59.     trigger_error('getDataIfExists() expects a minimum of 1 argument', E_USER_WARNING);
  60.     return NULL;
  61.   }
  62.  
  63.   // The object we are working with
  64.   $baseObj = array_shift($args);
  65.  
  66.   // Check it actually is an object
  67.   if (!is_object($baseObj)) {
  68.     trigger_error('getDataIfExists(): first argument must be an object', E_USER_WARNING);
  69.     return NULL;
  70.   }
  71.  
  72.   // Loop subsequent arguments, check they are valid and get their value(s)
  73.   foreach ($args as $arg) {
  74.     $arg = (string) $arg;
  75.     if (substr($arg, -2) == '()') { // method
  76.       $arg = substr($arg, 0, -2);
  77.       if (!method_exists($baseObj, $arg)) return NULL;
  78.       $baseObj = $baseObj->$arg();
  79.     } else { // property
  80.       if (!isset($baseObj->$arg)) return NULL;
  81.       $baseObj = $baseObj->$arg;
  82.     }
  83.   }
  84.  
  85.   // If we get here $baseObj will contain the item referenced by the supplied chain
  86.   return $baseObj;
  87.  
  88. }
  89.  
  90. // Call it like this:
  91. $subObj = getDataIfExists($xml, 'assessment', 'outcomes_processing', 'outcomes', 'decvar', 'attributes()', 'cutvalue');