生成 XML 并将其保存到磁盘问题
当我生成 XML 文件并将其保存到磁盘时,收到以下错误消息。我收到“XML 文档必须具有顶级元素”。可能是什么问题?我很感激任何帮助。如果我使用
echo sqlToXml($result, "SalesOrder", "sales");
xml数据在IE中显示。但由于某种原因,当我尝试保存它时,它会抛出错误。
header("content-type:text/xml");
$connection = mysql_connect( $db_host, $db_username, $db_password );
if (!$connection){
die ("Could not connect to the database: <br />". mysql_error());
}
// Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: <br />". mysql_error());
}
$result = mysql_query("SELECT * from table1");
header("Content-Type: application/xml");
$xmlDocument1 = sqlToXml($result, "SalesOrder", "sales");
$filename= 'c:\\xmldoc\\results.xml';
file_put_contents($filename ,$xmlDocument1);
function sqlToXml($queryResult, $rootElementName, $childElementName)
{
$xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$xmlData .= "<" . $rootElementName . ">";
while($record = mysql_fetch_object($queryResult))
{
$xmlData .= "<" . $childElementName . ">";
for ($i = 0; $i < mysql_num_fields($queryResult); $i++)
{
$fieldName = mysql_field_name($queryResult, $i);
$xmlData .= "<" . $fieldName . ">";
if(!empty($record->$fieldName))
$xmlData .= $record->$fieldName;
else
$xmlData .= "null";
$xmlData .= "</" . $fieldName . ">";
}
$xmlData .= "</" . $childElementName . ">";
}
$xmlData .= "</" . $rootElementName . ">";
}
这是结果 XML。
<?xml version="1.0" encoding="ISO-8859-1" ?>
<SalesOrder>
<Sales>
<SalesOrderNumber>SO43</SalesOrderNumber>
<PurchaseOrderNumber>PO18</PurchaseOrderNumber>
<AccountNumber>3333</AccountNumber>
<OrderDate>2001-07-01</OrderDate>
</Sales>
</SalesOrder>
I am getting the following error message when I generate the XML file and save it to the disk. I am getting "XML document must have a top level element". What could be the problem? I appreciate any help. If I use
echo sqlToXml($result, "SalesOrder", "sales");
the xml data shows in IE. But for some reason it throws an error when I try to save it.
header("content-type:text/xml");
$connection = mysql_connect( $db_host, $db_username, $db_password );
if (!$connection){
die ("Could not connect to the database: <br />". mysql_error());
}
// Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: <br />". mysql_error());
}
$result = mysql_query("SELECT * from table1");
header("Content-Type: application/xml");
$xmlDocument1 = sqlToXml($result, "SalesOrder", "sales");
$filename= 'c:\\xmldoc\\results.xml';
file_put_contents($filename ,$xmlDocument1);
function sqlToXml($queryResult, $rootElementName, $childElementName)
{
$xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$xmlData .= "<" . $rootElementName . ">";
while($record = mysql_fetch_object($queryResult))
{
$xmlData .= "<" . $childElementName . ">";
for ($i = 0; $i < mysql_num_fields($queryResult); $i++)
{
$fieldName = mysql_field_name($queryResult, $i);
$xmlData .= "<" . $fieldName . ">";
if(!empty($record->$fieldName))
$xmlData .= $record->$fieldName;
else
$xmlData .= "null";
$xmlData .= "</" . $fieldName . ">";
}
$xmlData .= "</" . $childElementName . ">";
}
$xmlData .= "</" . $rootElementName . ">";
}
Here is the result XML.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<SalesOrder>
<Sales>
<SalesOrderNumber>SO43</SalesOrderNumber>
<PurchaseOrderNumber>PO18</PurchaseOrderNumber>
<AccountNumber>3333</AccountNumber>
<OrderDate>2001-07-01</OrderDate>
</Sales>
</SalesOrder>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否错过了 sqlToXml-Function 中的 return 语句?
Did you miss the return statement in your sqlToXml-Function?