Sunday, 11 January 2015

Creating a XML document



I have the following code which is displaying an xml page as with the URL 'ourcustomersdb.php'. I need this same information to be displayed with the URL ending in 'ourcustomers.xml'. (Hope that makes sense)


The code I have works, but I need the last line of code to create the xml document:



<?php
header ("Content-Type:text/xml");//Tell browser to expect xml
include ("config/init.php");
$query = "SELECT * FROM ourcustomers";
$result = mysqli_query($mysqli_conn, $query) or die ("Error in query: $query. ".mysql_error());
//Top of xml file
$_xml = '<?xml version="1.0"?>';
$_xml .="<ourcustomers>";
while($row = mysqli_fetch_array($result)) {
$_xml .="<ourcustomer>";
$_xml .="<id>".$row['id']."</id>";
$_xml .="<customer>".$row['customer']."</customer>";
$_xml .="<work_done>".$row['work_done']."</work_done>";
$_xml .="</ourcustomer>";
}
$_xml .="</ourcustomers>";
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
//output
print $xmlobj->asXML();
//write to a file
$xmlobj->asXML(ourcustomers.xml);
?>


The line that doesn't seem to be working is the $xmlobj->asXML(ourcustomers.xml). Can someone explain why as this is the first time I'm trying to do this so I'm not fully understanding it.


Any help would be great!


No comments:

Post a Comment