【问题标题】:How would I echo this XML data from a database using PHP simpleXML?如何使用 PHP simpleXML 从数据库中回显这些 XML 数据?
【发布时间】:2013-01-27 03:26:02
【问题描述】:

我在 Wordpress 中使用了一个表单构建器插件,它将表单输入作为 XML 数据提交到数据库。现在我想获取该数据并将其显示在另一个页面中。我已经开始尝试 simpleXML 来实现这一点,但现在我遇到了困难。

出现在数据库每一行的 XML 数据遵循这种格式:

<form>
    <FormSubject>Report</FormSubject>
    <FormRecipient>****@***.com</FormRecipient>
    <Name>admin</Name>
    <Department>test</Department>
    <Value>1000</Value>
    <Comments>test</Comments>
    <Page>http://***.com</Page>
    <Referrer>http://****.com</Referrer>
</form>

我之前已经设法使用 simpleXML 从数据库中相同标记的 XML 字符串中获取我需要的数据,但现在我的问题是,我如何使用循环为数据库中的每一行执行此操作?

以下代码运行时,wordpress显示空白页,表示有错误:

<?php
global $wpdb;
$statistics = $wpdb->get_results("SELECT * FROM wpformbuilder_results WHERE form_id = '00000000000000000001';");
echo "<table>";
foreach($statistics as $statistic){
$string = $statistic->xmldata
$xml = simplexml_load_string($string);
$Name = (string) $xml->Name;
$Department = (string) $xml->Department;
$Value = (string) $xml->Value;
$Comments = (string) $xml->Comments;

echo "<tr>";
echo "<td>".$statistic->timestamp."</td>";
echo "<td>".$Name."</td>";
echo "<td>".$Department."</td>";
echo "<td>".$Value."</td>";
echo "<td>".$Comments."</td>";
echo "</tr>";
}
echo "</table>";

?>

【问题讨论】:

    标签: php xml wordpress simplexml


    【解决方案1】:

    你不见了;在第 5 行

    $string = $statistic->xmldata
    

    应该是

    $string = $statistic->xmldata;
    

    您应该考虑在 wp-config.php 文件中启用 WP_DEBUG 常量。将以下代码插入到您的 wp-config.php 中,就在 /* 之前,就是这样,停止编辑!快乐的博客。 */

    define('WP_DEBUG', true);
    
    /* That's all, stop editing! Happy blogging. */
    

    有关调试的更多提示,请阅读codex

    Formbuilder 用户自定义函数在 formbuilder_xml_db_results 类中提取 XML 数据:

    function xmltoarray($xml)
        {
            $xml = trim($xml);
    
            $match = "#<([a-z0-9_]+)([ \"']*[a-z0-9_ \"']*)>(.*)(</\\1>)#si";
            $offset = 0;
    
            if(!preg_match($match, $xml, $regs, false, $offset)) {
                return($xml);
            }
    
            while(preg_match($match, $xml, $regs, false, $offset))
            {
                list($data, $element, $attribs, $content, $closing) = $regs;
                $offset = strpos($xml, $data) + strlen($data);
    
                $tmp = $this->xmltoarray($content);
                $result[$element] = $tmp;
    
            }
    
            return($result);
        }
    

    在您的代码中定义该函数(在全局 $wpdb 之前;您不必害怕与该函数在 Class 中定义的名称相同),然后以这种方式修改您的代码:

    <?php
    global $wpdb;
    $statistics = $wpdb->get_results("SELECT * FROM wpformbuilder_results WHERE form_id = '00000000000000000001';");
    echo "<table>";
    foreach($statistics as $statistic){
    $xml = xmltoarray( $statistic->xmldata );
    $Name = (string) $xml['form']['Name'];
    $Department = (string) $xml['form']['Department'];
    $Value = (string) $xml['form']['Value'];
    $Comments = (string) $xml['form']['Comments'];
    
    echo "<tr>";
    echo "<td>".$statistic->timestamp."</td>";
    echo "<td>".$Name."</td>";
    echo "<td>".$Department."</td>";
    echo "<td>".$Value."</td>";
    echo "<td>".$Comments."</td>";
    echo "</tr>";
    }
    echo "</table>";
    
    ?>
    

    编辑:将 $xml['Comments'] 编辑为 $xml['form']['Comments'] 和类似的

    【讨论】:

    • 我添加了分号,但现在通过调试,我得到三个不同的错误,不断重复自己:警告:simplexml_load_string():实体:第 1 行:解析器错误:字符串未启动期待 ' 或 " 在 /###.php 第 34 行警告:simplexml_load_string(): in /###.php 第 34 行Warning: simplexml_load_string(): ^ in /###.php 第 34 行 其中第 34 行是 $xml = simplexml_load_string($string);
    • 我已修改我的答案以匹配您的新问题。问题是存储的数据不是正确的 XML 格式...
    【解决方案2】:

    我通过使用stripslashes()从 XML 字符串中去除反斜杠来修复它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 2011-02-27
      • 2017-05-17
      • 1970-01-01
      相关资源
      最近更新 更多