Reputation: 783
I've had this issue with an XML File i've been trying to echo out using PHP Without making 1 long line of text.
I run this code first to create my XML page, of all the products in my database:
<?php
$cfg = Array();
$cfg['db_host'] = '***';
$cfg['db_user'] = '***';
$cfg['db_password'] = '***';
$cfg['db_name'] = '***';
mysql_connect($cfg['db_host'], $cfg['db_user'], $cfg['db_password']);
mysql_select_db($cfg['db_name']);
unset($cfg);
$result = mysql_query("SELECT * FROM Artikelen");
$xml = new SimpleXMLElement('<xml/>');
while($row = mysql_fetch_assoc($result)) {
$draw = $xml->addChild('draw');
$draw->addChild('Product_Id',$row['Pr_Id']);
$draw->addChild('Product',$row['Product']);
$draw->addChild('Prijs',$row['Prijs']);
}
$fp = fopen("Artikelen.xml","wb");
fwrite($fp,$xml->asXML());
fclose($fp);
?>
Then i run this code from another file to show the .XML
:
<?php
$xml=simplexml_load_file("Artikelen.xml");
print_r($xml);
?>
What this shows is:
SimpleXMLElement Object ( [draw] => Array ( [0] => SimpleXMLElement Object ( [Product_Id] => 1 [Product] => Pizza Margherita [Prijs] => 7.00 )))
Times 11 in 1 long line, since i have 11 products. I've tried some things as:
<?php
$xml=simplexml_load_file("Artikelen.xml");
echo $xml->Product_Id . "<br>";
echo $xml->Product . "<br>";
echo $xml->Prijs . "<br><br>";
?>
But this doesn't seem to work.
This is my first time working with XML files, so it might be a very simple solution.
Thanks in Advance!, If you seem to miss information please let me know.
Upvotes: 0
Views: 1847
Reputation: 74
No need to re-invent the wheel. Echo file_get_contents(), as in the below solution.
https://stackoverflow.com/a/1199643/2257899
Edit:
If you look at your print_r() results, you'll see it contains an array. Try:
<?php
$xml=simplexml_load_file("Artikelen.xml");
echo $xml["Product_Id"] . "<br>";
echo $xml["Product"] . "<br>";
echo $xml["Prijs"] . "<br><br>";
?>
Upvotes: 1
Reputation: 74
If you want to stick with print_r(), just echo pre tags before and after.
eg:
<?php
$xml=simplexml_load_file("Artikelen.xml");
echo '<pre>';
print_r($xml);
echo '</pre>';
?>
Upvotes: 1
Reputation: 1993
You can display xml using asXml method:
$xml=simplexml_load_file("Artikelen.xml");
echo $xml->asXML();
Upvotes: 0