【问题标题】:XML output: A name was started with an invalid characterXML 输出:名称以无效字符开头
【发布时间】:2017-02-15 10:24:05
【问题描述】:

当我回显 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...

【问题讨论】:

    标签: php xml


    【解决方案1】:

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

    如果您正在使用 API,您也应该会收到更好的错误消息。

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      相关资源
      最近更新 更多