0

I am trying to export data from MySQL table to XML file.

I am using Adobe Dreamweaver CC 2015 to create index.php file contains the below code:

<?php
header('Content-type: text/xml');

$xmlout = "<?xml version=\"1.0\" ?>";
$xmlout .= "<list>";

$db = new PDO('mysql:host=hostname;dbname=dbname','dbusername','password');
$stmt = $db->prepare("select * from tablename");
$stmt->execute();
while($row = $stmt->fetch()){
 $xmlout .= "\t<persons>\n";
 $xmlout .= "\t\t<id>".$row['ID']."</id>\n";
 $xmlout .= "\t\t<name>".$row['name']."</name >\n";
 $xmlout .= "\t\t<phone>".$row['phone']."</phone >\n";
 $xmlout .= "\t\t<address>".$row['address']."</address >\n";
 $xmlout .= "\t\t<status>".$row['status']."</status>\n";
 $xmlout .= "\t</persons>\n";
}

$xmlout .= "</list>";
echo $xmlout;
?>

when I run the page I find this error:

error on line 10 at column 8: Extra content at the end of the document

the result in chrome Inspect > Sources > index.php

<?xml version="1.0" ?>
<list>  
    <persons>
        <id>1</id>
        <name>John</name>
        <phone>123456</phone>
        <address>123 street</address>
        <status>Ready</status>
    </persons>
</list>

but there is a . after and when I tried to use http://www.xmlvalidation.com/ to validate my XML output I found that

</list>&#65279;

what should I do to fix this error?

Parfait
  • 104,375
  • 17
  • 94
  • 125
Triple M
  • 39
  • 1
  • 4

3 Answers3

0

after seeing this post the problem solved. Extra content at the end of the document PHP and XML

and because I am using MAC that makes me try https://hexed.it/ as Hex Editor to remove the Extra content at the end of the document.

Community
  • 1
  • 1
Triple M
  • 39
  • 1
  • 4
0

You're generating the XML and you're not escaping the data from the database. That means any character with special meaning in XML will break the output.

Be careful an escape values using htmlspecialchars() if you're generating XML/HTML output.

Or even better use an XML API like DOM or XMLWriter to generate the XML and let the API take care of the serialization.

PHP files not not need the closing ?> at the end of the file any more. You can omit it and avoid whitespaces at the end of your output.

ThW
  • 19,120
  • 3
  • 22
  • 44
0

Consider using a dedicated W3C-compliant XML library like PHP's DOMDocument class to build XML content and not from concatenated text strings. With such methods as createElement, appendChild, setAttribute as well as formatOutput and preserverwhiteSpace properties, you avoid the need to concatenate special characters such as line breaks, tabs, and angle brackets.

It should be noted, XML is not a raw text file like csv/txt but a standardized, structured document with encoding and markup rules to describe data.

header('Content-type: text/xml');

// INITIALIZE DOM OBJECT
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;            
$dom->preserveWhiteSpace = false;

// CREATE ROOT AND APPEND TO DOCUMENT
$xmlRoot = $dom->createElement("list");
$xmlRoot = $dom->appendChild($xmlRoot);

// QUERY DATABASE
$db = new PDO('mysql:host=hostname;dbname=dbname','dbusername','password');
$stmt = $db->prepare("select * from tablename");
$stmt->execute();

// FETCH ROWS ITERATIVELY
while($row = $stmt->fetch()){
     // APPEND PERSON AS CHILD OF ROOT
     $personNode = $xmlRoot->appendChild($dom->createElement('person'));

     // APPEND CHILDREN TO PERSON         
     $personNode->appendChild($dom->createElement('id', $row['id']));
     $personNode->appendChild($dom->createElement('name', $row['name']));
     $personNode->appendChild($dom->createElement('phone', $row['phone']));
     $personNode->appendChild($dom->createElement('address', $row['address']));
     $personNode->appendChild($dom->createElement('status', $row['status']));
}
$stmt = null; 
$db = null;

// OUTPUT TO SCREEN
echo $dom->saveXML();
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • thanks @Parfait for your mass explanation, but if you can help me in another situation, how can I load data from other tables to the person node? you can see this Url as reference to understand what i mean. http://www.informit.com/articles/article.aspx?p=27800 – Triple M Apr 23 '17 at 15:12
  • Haha...great to hear from you! And glad it helped! For other tables, consider joining them in the SQL if they relate and extend the fetch loop here. – Parfait Apr 23 '17 at 15:34
  • @ Parfait please check my post below – Triple M Apr 23 '17 at 16:41