在php中编写xml文件时收到关于xmlelement的警告
我使用以下代码在php中编写xml文件,但当我运行此代码时,会出现以下错误在php中编写xml文件时收到关于xmlelement的警告,php,xml,Php,Xml,我使用以下代码在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
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);
?>
如何更正错误。请帮助我验证mysql连接的编码/字符集。它可以在活动连接上设置,也可以在数据库服务器配置中配置
请参阅:,了解这些作业中的至少一项:
$mydata->loc = $row['Siteurl'];
正在使用$row['Siteurl']
一个不包含UTF-8编码数据的字符串
对于所有赋值,SimpleXMLElement希望数据以Unicode编码为UTF-8
但是,simplexmlement只会告诉您使用asXML()方法的时刻(在您的例子中是$xml->asXML()
)
如果您现在想找出您分配的哪个字符串数据是无效的UTF-8,您必须在分配之前通过检查每个字符串来自己验证这一点
您应该在现场现有的问答材料中找到一些有用的信息,例如: