使用PHP-HTML编码和常规错误代码将MySQL数据导出为xml

使用PHP-HTML编码和常规错误代码将MySQL数据导出为xml,php,html,xml,encoding,cdata,Php,Html,Xml,Encoding,Cdata,任何能回答我问题的人都应该获得一枚巨大的令人敬畏的金牌 我正在尝试将MySQL表的内容转换成一种漂亮、简单的XML格式。我正在运行一点PHP,它工作得很好,我可以看到XML(好时光)。但是,在MySQL表中有几个字段填充了未编码的HTML表代码。我在包装CDATA标记中得到的每个字段值,我确保xml标记被关闭,但我想知道我是否因为出错而遗漏了什么,我不知道为什么(糟糕的时候)。我觉得很好,所以我试着在Excel中打开它(客户会这么看),它声称“package”的开始标记与“long_descri

任何能回答我问题的人都应该获得一枚巨大的令人敬畏的金牌

我正在尝试将MySQL表的内容转换成一种漂亮、简单的XML格式。我正在运行一点PHP,它工作得很好,我可以看到XML(好时光)。但是,在MySQL表中有几个字段填充了未编码的HTML表代码。我在包装CDATA标记中得到的每个字段值,我确保xml标记被关闭,但我想知道我是否因为出错而遗漏了什么,我不知道为什么(糟糕的时候)。我觉得很好,所以我试着在Excel中打开它(客户会这么看),它声称“package”的开始标记与“long_description”的结束标记匹配

是我正在处理的页面,因此您可以在那里查看数据

这是我正在使用的代码

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


        if(mysql_num_rows($result)>0)
        {
           while($result_array = mysql_fetch_assoc($result))
           {
              $xml .= "<package>";

              //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.="</package>";
           }
        }

//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=”“;
$root\u element=“packages”;
$xml.=“”;
如果(mysql_num_rows($result)>0)
{
而($result\u array=mysql\u fetch\u assoc($result))
{
$xml.=“”;
//循环遍历行中的每个键、值对
foreach($result\u数组为$key=>$value)
{
//$key保存表的列名
$xml.=“”;
//将SQL数据嵌入CDATA元素以避免XML实体问题
$xml.=“”;
//然后关闭元素
$xml.=“”;
}
$xml.=“”;
}
}
//关闭根元素
$xml.=“”;
//将xml标头发送到浏览器
标题(“内容类型:text/xml”);
//输出XML数据
echo$xml;
我到底做错了什么


编辑 好的,似乎有什么东西删除了一个“long_description>”——发生了一些非常奇怪的事情,标签的格式不正确。我不完全确定这是怎么发生的,因为我使用的代码肯定会在所有$key上加上开始和结束的尖括号。它只发生一次,但似乎把其他的事情都塞进了肚子

有人知道为什么会这样吗

非常感谢您的帮助,并提前表示感谢

Gem

我觉得这将是解决这个问题的方法。乍一看,代码可能有点麻烦,但它将为您处理所有的输出格式:

<?php

  // The names of the root node and the node that will contain a row
  $root_element = "packages";
  $row_element = "package";

  // Create the DOMDocument and the root node
  $dom = new DOMDocument('1.0', 'utf-8');
  $rootNode = $dom->appendChild($dom->createElement($root_element));

  // Loop the DB results
  while ($row = mysql_fetch_assoc($result)) {

    // Create a row node
    $rowNode = $rootNode->appendChild($dom->createElement($row_element));

    // Loop the columns
    foreach ($row as $col => $val) {

      // Create the column node and add the value in a CDATA section
      $rowNode->appendChild($dom->createElement($col))
              ->appendChild($dom->createCDATASection($val));

    }

  }

  // Output as string
  echo $dom->saveXML();

您确定xml正在工作吗?它返回
XML解析错误:格式不正确
near
3x day Stag hunt
。是的,最后一个包(红色Stag包)有问题-和元素格式不正确-是的-有东西正在吞噬上的一个开始标记(对不起,键盘被捣烂了…)这一行的详细描述:…每日费率。

]]>10。。。奇怪…还有你们提到的那个问题。。。!如果我现在能证明我爱你的方式,我会的。谢谢,这让我们看到了一个充满痛苦的世界。万岁的达维兰多姆等。