I am trying to display xml in browser.i have created a xml like
$query2 = "SELECT * FROM user where user_id = 7";
$dbresult = mysqli_query($dbcon,$query2) or die('query error...');
$doc = new DomDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
while($row = mysqli_fetch_array($dbresult)) {
$userId = $doc->createElement('UserId');
$userId = $root->appendChild($userId);
$value = $doc->createTextNode($row[0]);
$value = $userId->appendChild($value);
$psw = $doc->createElement('Password');
$psw = $root->appendChild($psw);
$value = $doc->createTextNode($row[2]);
$value = $psw->appendChild($value);
}
echo $doc->saveXML();
$doc->save("test.xml");
it displaying just data like
7 pwd123
but i want data like
<xml>
<root>
<userId>7</userId>
<password>pwd123</password>
</root>
</xml>
what to do for that? thanks
use Right Click -> Show source code command
Your script doesn't set the Content-type header and php sends the default content-type header which usually is Content-type: text/html
.
The client therefore assumes that the data is an html document and parses and displays it as such.
And if the client/browser doesn't "know" the tags/elements your document contains (and context matters) http://www.w3.org/TR/html401/appendix/notes.html#notes-invalid-docs applies.
If you "tell" the client that the output is an xml document it will/can/should display it accordingly.
if ( headers_sent() ) {
// can't set the content-type after output has been sent to the client
die('error');
}
else {
header('Content-type: text/xml'); // see http://en.wikipedia.org/wiki/XML_and_MIME
echo $doc->saveXML();
}
Change your header content-type text/xml
header('Content-type: text/xml');
$doc->saveXML();
For more details refer http://www.w3schools.com/xml/xml_server.asp