MySQL::GetXML PHP Method

GetXML() public method

Returns the last query as an XML Document
public GetXML ( ) : string
return string XML containing all records listed
    public function GetXML()
    {
        // Create a new XML document
        $doc = new DomDocument('1.0');
        // ,'UTF-8');
        // Create the root node
        $root = $doc->createElement('root');
        $root = $doc->appendChild($root);
        // If there was a result set
        if (is_resource($this->last_result)) {
            // Show the row count and query
            $root->setAttribute('rows', $this->RowCount() ? $this->RowCount() : 0);
            $root->setAttribute('query', $this->last_sql);
            $root->setAttribute('error', "");
            // process one row at a time
            $rowCount = 0;
            while ($row = mysqli_fetch_assoc($this->last_result)) {
                // Keep the row count
                $rowCount = $rowCount + 1;
                // Add node for each row
                $element = $doc->createElement('row');
                $element = $root->appendChild($element);
                $element->setAttribute('index', $rowCount);
                // Add a child node for each field
                foreach ($row as $fieldname => $fieldvalue) {
                    $child = $doc->createElement($fieldname);
                    $child = $element->appendChild($child);
                    // $fieldvalue = iconv("ISO-8859-1", "UTF-8", $fieldvalue);
                    $fieldvalue = htmlspecialchars($fieldvalue);
                    $value = $doc->createTextNode($fieldvalue);
                    $value = $child->appendChild($value);
                }
                // foreach
            }
            // while
        } else {
            // Process any errors
            $root->setAttribute('rows', 0);
            $root->setAttribute('query', $this->last_sql);
            if ($this->Error()) {
                $root->setAttribute('error', $this->Error());
            } else {
                $root->setAttribute('error', "No query has been executed.");
            }
        }
        // Show the XML document
        return $doc->saveXML();
    }