You are here

public static function ApacheSolrDocument::documentToXml in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 Apache_Solr_Document.php \ApacheSolrDocument::documentToXml()
  2. 6.3 Apache_Solr_Document.php \ApacheSolrDocument::documentToXml()

Create an XML fragment from a ApacheSolrDocument instance appropriate for use inside a Solr add call

Parameters

ApacheSolrDocument $document:

Return value

string an xml formatted string from the given document

1 call to ApacheSolrDocument::documentToXml()
DrupalApacheSolrService::addDocuments in ./Drupal_Apache_Solr_Service.php
Add an array of Solr Documents to the index all at once

File

./Apache_Solr_Document.php, line 355

Class

ApacheSolrDocument
Holds Key / Value pairs that represent a Solr Document along with any associated boost values. Field values can be accessed by direct dereferencing such as:

Code

public static function documentToXml(ApacheSolrDocument $document) {
  $xml = '<doc';
  if ($document
    ->getBoost() !== FALSE) {
    $xml .= ' boost="' . $document
      ->getBoost() . '"';
  }
  $xml .= '>';
  foreach ($document as $key => $value) {
    $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8');
    $fieldBoost = $document
      ->getFieldBoost($key);
    if (is_array($value)) {
      foreach ($value as $multivalue) {
        $xml .= '<field name="' . $key . '"';
        if ($fieldBoost !== FALSE) {
          $xml .= ' boost="' . $fieldBoost . '"';

          // Only set the boost for the first field in the set
          $fieldBoost = FALSE;
        }
        $xml .= '>' . htmlspecialchars($multivalue, ENT_NOQUOTES, 'UTF-8') . '</field>';
      }
    }
    else {
      $xml .= '<field name="' . $key . '"';
      if ($fieldBoost !== FALSE) {
        $xml .= ' boost="' . $fieldBoost . '"';
      }
      $xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8') . '</field>';
    }
  }
  $xml .= '</doc>';

  // Remove any control characters to avoid Solr XML parser exception
  return self::stripCtrlChars($xml);
}