You are here

class wsdata_simple_xml_processor in Web Service Data 7

Hierarchy

Expanded class hierarchy of wsdata_simple_xml_processor

1 string reference to 'wsdata_simple_xml_processor'
wsdata_example_wikipedia_field_default_fields in modules/ws_examples/wsdata_example_wikipedia/wsdata_example_wikipedia.features.field.inc
Implements hook_field_default_fields().

File

./wsdata_simple_xml.wsdata.processor.inc, line 3

View source
class wsdata_simple_xml_processor extends WsData {

  // Parse the web service response string, and returns a structured data array
  public function parse($data) {
    if (!isset($data) || empty($data)) {
      return;
    }
    $data = trim($data);
    libxml_use_internal_errors(TRUE);
    try {
      $data = new SimpleXMLElement($data);
      if ($data
        ->count() == 0) {
        return array(
          $data
            ->getName() => $data
            ->__toString(),
        );
      }
      $data = get_object_vars($data);
      foreach ($data as $key => $value) {
        $data[$key] = $this
          ->_parsexml($value);
      }
    } catch (exception $e) {
      return FALSE;
    }
    libxml_use_internal_errors(FALSE);
    return $data;
  }
  function accepts() {
    return array(
      'xml',
    );
  }

  // XML Parsing helper function, converts nested XML objects into arrays
  private function _parsexml($value) {
    if (is_object($value) and get_class($value)) {
      $value = get_object_vars($value);
      foreach ($value as $k => $v) {
        $value[$k] = $this
          ->_parsexml($v);
      }
    }
    elseif (is_array($value)) {
      foreach ($value as $key => $xml) {
        $value[$key] = $this
          ->_parsexml($xml);
      }
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WsData::$data public property
WsData::$error protected property
WsData::$languages protected property
WsData::addData public function Add data to an empty object or replace all existing data
WsData::getData public function Retrieve the value for the given data key. 1
WsData::getError public function
WsData::__construct public function
wsdata_simple_xml_processor::accepts function Overrides WsData::accepts
wsdata_simple_xml_processor::parse public function Overrides WsData::parse
wsdata_simple_xml_processor::_parsexml private function