【问题标题】:Incorrect XML format output不正确的 XML 格式输出
【发布时间】:2015-08-28 09:32:57
【问题描述】:

下面提到的代码能够从数据库中获取数据,输出也是 XML 格式,但我想要的顺序不正确。

<?php
    $config['mysql_host'] = "localhost";
    $config['mysql_user'] = "market";
    $config['mysql_pass'] = "12345";
    $config['db_name']    = "marketing";
    $config['table_name'] = "data";
    $root = customers; 

    //connect to host
    mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
    //select database
    @mysql_select_db($config['db_name']) or die( "Unable to select database");

    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = $root; 
    $xml         .= "<$root_element>";

    //select all items in table
    $sql = "SELECT * FROM ".$config['table_name'];

    $result = mysql_query($sql);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    if(mysql_num_rows($result)>0)
    {
       while($result_array = mysql_fetch_assoc($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.=$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;
    ?>

当前输出:

<customers>
<data>
<customer>111</customer>
<openingbal>111</openingbal>
<pandl>11</pandl>
<currentbal>111</currentbal>
<time>2015-08-28 04:07:21</time>
</data>
<data>
<customer>111</customer>
<openingbal>111</openingbal>
<pandl>11</pandl>
<currentbal>111</currentbal>
<time>2015-08-28 04:07:21</time>
</data>
</customers>

但我想要下面的输出,其中id = "something"&gt; 但上面的代码不会产生相同的结果。

<Customers>
  <Customer ID ="111">
    <openingbal>111</openingbal>
    <pandl>11</pandl>
    <currentbal>111</currentbal>
    <time>2015-08-14 03:14:30</time>
  </Customer>
  <Customer Id = "111">
    <openingbal>111</openingbal>
    <pandl>11</pandl>
    <currentbal>111</currentbal>
    <time>2015-08-14 03:14:30</time>
  </Customer>
</Customers>

【问题讨论】:

  • 嗨,桑德比。感谢您的回复。我是 xml 新手。你能帮忙在哪里更改代码吗?但我无法与你发送的链接联系起来。
  • 看看我的回答,我觉得这样就可以了

标签: php xml xml-parsing


【解决方案1】:

这样就可以了。

<?php
    $config['mysql_host'] = "localhost";
    $config['mysql_user'] = "market";
    $config['mysql_pass'] = "12345";
    $config['db_name']    = "marketing";
    $config['table_name'] = "data";
    $config['main_key'] = "customer";
    $root = 'Customers'; 

    //connect to host
    mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
    //select database
    @mysql_select_db($config['db_name']) or die( "Unable to select database");

    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = $root; 
    $xml         .= "<$root_element>";

    //select all items in table
    $sql = "SELECT * FROM ".$config['table_name'];

    $result = mysql_query($sql);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    if(mysql_num_rows($result)>0)
    {
       while($result_array = mysql_fetch_assoc($result))
       {
          $xml .= "<".ucfirst($config['main_key']);

          //loop through each key,value pair in row
          foreach($result_array as $key => $value)
          {

             if ($key == $config['main_key'])  {
                  $xml .= " ID = \"".$value."\">";
             }else    {
                 //$key holds the table column name
                 $xml .= "<$key>";

                 //embed the SQL data in a CDATA element to avoid XML entity issues
                 $xml.=$value;

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

          $xml.="</".ucfirst($config['main_key']).">";
       }
    }

    //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;
    ?>

【讨论】:

  • XML 现在看起来如何?什么或谁给出了这个错误?
  • phpmyadmin 给出了错误。它没有打印 xml,但抛出了错误。
  • nops 。还是行不通 。在 IE 页面中未打开但在 google chrome 中的错误是“第 1 行第 57 列的错误:错误解析属性名称”
  • 你能以某种方式显示生成的 XML 吗?我已经尝试了代码,它在我的情况下按预期工作。
  • 嘿,桑德比。是的,它现在正在工作。从我这边到最后都有错误。我没有关闭。多谢 。 :)
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多