2015-01-11 147 views
0

我有以下代码显示一个xml页面,就像URL'ourcustomersdb.php'一样。我需要以“ourcustomers.xml”结尾的URL显示相同的信息。 (希望是有道理的)从php页面创建XML文档

我作品的代码,但我需要的最后一行代码来创建XML文档:

<?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); 
?> 

似乎并不奏效的线是$ xmlobj-> asXML(ourcustomers.xml)。我需要这个从数据库收集的信息来创建一个XML文件。如果我在最后一次打印评论中留言,它会显示XML,但是在页面ourcustomersdb.php中,但我需要将它显示在ourcustomers.xml中(希望这是有道理的!)。有人可以解释为什么,因为这是我第一次尝试这样做,所以我没有完全理解它。

任何帮助将是伟大的!

+0

帮助:启用错误报告,并记录到你的开发机器上的最高水平,首先将代码置于一个问题之前解决所有错误,警告,通知和严格的警告。此外,不要发布死代码(例如,您注释掉的那些代码行)。对于一个问题是非常有用的,不要发布你的实际代码,而是从头开始创建一个新的例子,用*作为必要的小代码展示你的问题*并且包含所有数据(并且根据需要尽可能少),所以很清楚你询问一下,你就不会分心。 – hakre

回答

1

您需要用引号括起ourcustomers.xml,以便它是一个字符串。否则,你会得到一个语法错误。

$xmlobj->asXML('ourcustomers.xml'); 

而且,你不必使用SimpleXMLElement写一个XML文件,你可以只使用file_put_contents

file_put_contents('ourcustomers.xml', $_xml); 
+0

这就是它的工作。我以为我尝试过,但我一定忘了评论'印刷'。谢谢! – alwaystrying