【发布时间】:2014-07-21 07:24:39
【问题描述】:
我有以下代码,我用它在 php 中编写 xml 文件,但是当我运行此代码时,我得到以下错误
SimpleXMLElement::asXML(): string is not in UTF-8
生成xml的代码如下
<?php
//Create Database connection
$mysqli = new mysqli('localhost', 'user', 'pass', 'dbnam');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
}
$query='select Siteurl from tablename order by colname2 desc ';
$result = mysqli_query($mysqli,$query);
//Create SimpleXMLElement object
$xml = new SimpleXMLElement('<urls/>');
//Add each column value a node of the XML object
while($row = mysqli_fetch_array($result)) {
$mydata = $xml->addChild('url');
$mydata->loc=$row['Siteurl'];
}
mysqli_close($mysqli);
//Create the XML file
$fp = fopen("sitemaps/sitemap2.xml","wb");
//Write the XML nodes
fwrite($fp,$xml->asXML());
//Close the database connection
fclose($fp);
?>
我该如何纠正错误,请帮助我
【问题讨论】: