You are here

protected function EasyRdf_Sparql_Result::parseXml in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Result.php \EasyRdf_Sparql_Result::parseXml()

Parse a SPARQL result in the XML format into the object.

@ignore

1 call to EasyRdf_Sparql_Result::parseXml()
EasyRdf_Sparql_Result::__construct in vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Result.php
Create a new SPARQL Result object

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Result.php, line 269

Class

EasyRdf_Sparql_Result
Class for returned for SPARQL SELECT and ASK query responses.

Code

protected function parseXml($data) {
  $doc = new DOMDocument();
  $doc
    ->loadXML($data);

  # Check for valid root node.
  if ($doc
    ->hasChildNodes() == false or $doc->childNodes->length != 1 or $doc->firstChild->nodeName != 'sparql' or $doc->firstChild->namespaceURI != self::SPARQL_XML_RESULTS_NS) {
    throw new EasyRdf_Exception("Incorrect root node in SPARQL XML Query Results format");
  }

  # Is it the result of an ASK query?
  $boolean = $doc
    ->getElementsByTagName('boolean');
  if ($boolean->length) {
    $this->type = 'boolean';
    $value = $boolean
      ->item(0)->nodeValue;
    $this->boolean = $value == 'true' ? true : false;
    return;
  }

  # Get a list of variables from the header
  $head = $doc
    ->getElementsByTagName('head');
  if ($head->length) {
    $variables = $head
      ->item(0)
      ->getElementsByTagName('variable');
    foreach ($variables as $variable) {
      $this->fields[] = $variable
        ->getAttribute('name');
    }
  }

  # Is it the result of a SELECT query?
  $resultstag = $doc
    ->getElementsByTagName('results');
  if ($resultstag->length) {
    $this->type = 'bindings';
    $results = $resultstag
      ->item(0)
      ->getElementsByTagName('result');
    foreach ($results as $result) {
      $bindings = $result
        ->getElementsByTagName('binding');
      $t = new stdClass();
      foreach ($bindings as $binding) {
        $key = $binding
          ->getAttribute('name');
        foreach ($binding->childNodes as $node) {
          if ($node->nodeType != XML_ELEMENT_NODE) {
            continue;
          }
          $t->{$key} = $this
            ->newTerm(array(
            'type' => $node->nodeName,
            'value' => $node->nodeValue,
            'lang' => $node
              ->getAttribute('xml:lang'),
            'datatype' => $node
              ->getAttribute('datatype'),
          ));
          break;
        }
      }
      $this[] = $t;
    }
    return $this;
  }
  throw new EasyRdf_Exception("Failed to parse SPARQL XML Query Results format");
}