XML输出:名称以无效字符开头

时间:2017-02-15 10:24:06

标签: php xml

当我回显xml输出时,我得到标题中的错误。我忘记了什么? 开口标签不对吗?我希望有人可以帮我解决这个问题。

我按照网站http://www.mightywebdeveloper.com/coding/mysql-to-xml-php/

的说明进行操作

提前完成。

<?php



$config['table_name'] = "tblPortalStatus";
//database configuration   
$serverName = "servername";
$connectionInfo = array( all info here);
$conn = sqlsrv_connect( $serverName, $connectionInfo);



$sql = "SELECT * FROM tblPortalStatus";

$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

$result = sqlsrv_query($conn, $sql, $params,$options);     




$xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."s"; //tblPortalStatus
$xml         .= "<$root_element>";



if (!sqlsrv_num_rows($result)) {
    die('Invalid query: ' . sqlsrv_error());
}

if(sqlsrv_num_rows($result)>0)
{
   while($result_array = sqlsrv_fetch_array($result))
   {
      $xml .= "<".$config['table_name'].">";

      //loop through each key,value pair in row
      foreach($result_array as $key => $value)
      {
         //$key holds the table column name
         $xml .= "<$key>";

         //embed the SQL data in a CDATA element to avoid XML entity issues
         $xml .= "<![CDATA[$value]]>";

         //and close the element
         $xml .= "</$key>";
      }

      $xml.= "</".$config['table_name'].">";
   }
}     


//close the root element
$xml .= "</$root_element>";

//send the xml header to the browser
header ("Content-Type:text/xml");

//output the XML data
echo $xml;
?> 



The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 


--------------------------------------------------------------------------------

A name was started with an invalid character. Error processing resource 'http://PHP/Form.php'. Line 37, ...

<?xml version="1.0" encoding="UTF-8"?><tblPortalStatuss><tblPortalStatus><0><![CDATA[EPRC]]>&l...

1 个答案:

答案 0 :(得分:0)

使用XML Api(如XMLWriter或DOM)生成XML。这将避免大多数(但不是全部)问题。在将字段名称用作标记名称之前,可能需要对字段名称进行规范化,字段可以包含标记名称中不允许的字符(如空格)。

如果您使用的是API,那么您也应该获得更好的错误消息。