I'm trying to export some data from the database to XML file and then send the file to the browser (downloadable) , i can now download the file , but some data are missing !
here is the the part of the code where the problem exists:
<?php
include('session.php');
if (isset($_POST['exportxml'])) {
$filename = 'Data_Backup_' . date('m-d-Y_his') . '.xml';
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
$xml = new XMLWriter();
header('Content-Disposition: attachment;filename=' . $filename);
header('Content-Type: text/xml');
$xml->openURI("php://output");
$xml->setIndent(true);
$xml->startElement('data');
if (isset($_POST['exportaccounts'])) {
$query = "SELECT * from users";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$xml->startElement("useraccount");
$xml->writeElement("id", $row['id']);
$xml->writeElement("username", $row['username']);
$xml->writeElement("email", $row['email']);
$xml->writeElement("state", $row['state']);
$xml->endElement();
}
}
}
if (isset($_POST['exporttimzezone'])) {
$query7 = "SELECT * from options where optionname = 'timezone'";
$result7 = mysqli_query($conn, $query7);
if (mysqli_num_rows($result7) > 0) {
while ($row7 = mysqli_fetch_assoc($result7)) {
$xml->startElement("timezone");
$xml->writeElement("id", $row7['id']);
$xml->writeElement("optionname", $row7['optionname']);
$xml->writeElement("value", $row7['value']);
$xml->writeElement("state", $row7['state']);
$xml->endElement();
}
}
}
$xml->startElement("version");
$xml->writeElement("id", "1.0");
$xml->endElement();
$xml->endElement();
echo $domtree->saveXML();
exit;
$xml->flush();
?>
I don't know what is the cause of missing data problem because i never dealt with XML functions in php before .
P.S: all the connections to the database are successful and all the data exist and fetched without any problem from the tables
Regards
CodePudding user response:
You are getting your DOMDocument and XMLWriter confused.
If you do this, the only DOMDocument relevant lines of code in your script ...
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
echo $domtree->saveXML();
You get a valid XML Document but with nothing in it, and thats what you are sending to the client, an empty xml doc, after the actual XMLWriter document, so things are getting confused.
RESULT
<?xml version="1.0" encoding="UTF-8"?>
This may explain your missing data. So that whole $domtree stuff is irrelevant.
So all you need is the XMLWriter.
NOTE This line
$xml->openUri('php://stdout');
Says write the xml output directly to the client, this probably explains your issues with "Headers Already Sent...." as I assume you originally had the 2 header()
lines after the openUri()
call.
Sample code that writes the sort of data you want to the XMLWrites and therefore to the client. Note a couple of additional lines not in your original code to make the document complete
$xml = new XMLWriter();
$xml->openUri('php://stdout');
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('data');
// User
$xml->startElement("useraccount");
$xml->writeElement("id", 1);
$xml->writeElement("username", 'Fred');
$xml->writeElement("email", '[email protected]');
$xml->writeElement("state", 'NY');
$xml->endElement();
$xml->startElement("useraccount");
$xml->writeElement("id", 2);
$xml->writeElement("username", 'Bill');
$xml->writeElement("email", '[email protected]');
$xml->writeElement("state", 'NY');
$xml->endElement();
// Timezone
$xml->startElement("timezone");
$xml->writeElement("id", 3);
$xml->writeElement("optionname", 'Option 1');
$xml->writeElement("value", '99');
$xml->writeElement("state", 'CA');
$xml->endElement();
$xml->startElement("timezone");
$xml->writeElement("id", 3);
$xml->writeElement("optionname", 'Option 2');
$xml->writeElement("value", '11');
$xml->writeElement("state", 'TX');
$xml->endElement();
$xml->startElement("version");
$xml->writeElement("id", "1.0");
$xml->endElement();
$xml->endElement(); // data element
$xml->endDocument();
RESULT
<?xml version="1.0"?>
<data>
<useraccount>
<id>1</id>
<username>Fred</username>
<email>[email protected]</email>
<state>NY</state>
</useraccount>
<useraccount>
<id>2</id>
<username>Bill</username>
<email>[email protected]</email>
<state>NY</state>
</useraccount>
<timezone>
<id>3</id>
<optionname>Option 1</optionname>
<value>99</value>
<state>CA</state>
</timezone>
<timezone>
<id>3</id>
<optionname>Option 2</optionname>
<value>11</value>
<state>TX</state>
</timezone>
<version>
<id>1.0</id>
</version>
</data>
Now when you remove the DOMDocument stuff you should be sending a XMLDocument with all the data and only that.