【问题标题】:PHP SimpleXML function errorPHP SimpleXML 函数错误
【发布时间】:2012-02-10 10:15:00
【问题描述】:

我正在使用这个类将数组移植到 XML:

class ArrayToXML
{
    /**
     * The main function for converting to an XML document.
     * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
     *
     * @param array $data
     * @param string $rootNodeName - what you want the root node to be - defaultsto data.
     * @param SimpleXMLElement $xml - should only be used recursively
     * @return string XML
     */
    public static function toXml($data, $rootNodeName = 'data', &$xml=null)
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (ini_get('zend.ze1_compatibility_mode') == 1)
        {
            ini_set ('zend.ze1_compatibility_mode', 0);
        }

        if (is_null($xml))
        {
            $xml = simplexml_load_string("");
        }

        // loop through the data passed in.
        foreach($data as $key => $value)
        {
            // if numeric key, assume array of rootNodeName elements
            if (is_numeric($key))
            {
                $key = $rootNodeName;
            }

            // delete any char not allowed in XML element names
            $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);

            // if there is another array found recrusively call this function
            if (is_array($value))
            {
                // create a new node unless this is an array of elements
                $node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml;

                // recrusive call - pass $key as the new rootNodeName
                ArrayToXML::toXml($value, $key, $node);
            }
            else
            {
                // add single node.
                $value = htmlentities($value);
                $xml->addChild($key,$value);
            }

        }
        // pass back as string. or simple xml object if you want!
        return $xml->asXML();
    }

    // determine if a variable is an associative array
    public static function isAssoc( $array ) {
        return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
    }
}

我用这个代码示例调用这个函数只是为了测试一下:

require_once('test_xml.php');
$library = array(
    'book' => array(
        array(
            'authorFirst' => 'Mark',
            'authorLast' => 'Twain',
            'title' => 'The Innocents Abroad'
        ),
        array(
            'authorFirst' => 'Charles',
            'authorLast' => 'Dickens',
            'title' => 'Oliver Twist'
        )
    )
);
$ArrayToXml=new ArrayToXml();
$ArrayToXml->toXml($library)

我收到此错误:致命错误:在第 42 行调用非对象上的成员函数 addChild()。

知道出了什么问题吗? 谢谢。

编辑: 第 42 行:$node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml;

编辑2: 恕我直言,这段代码还没有工作。答案是正确的,但我只得到MarkTwainCharlesDickens 作为输出。我应该得到格式正确的 XML。我应该启用一些扩展吗?但是我已经看到simplexml被启用了。

感谢任何帮助。

【问题讨论】:

    标签: php simplexml


    【解决方案1】:

    您不能使用空字符串创建 XML 文档。

    var_dump(simplexml_load_string(""));
    bool(false)
    

    var_dump(simplexml_load_string("<root/>"));
    object(SimpleXMLElement)#1 (0) {}
    

    要从您的$data 中获取book 值:

    $root = key($data);
    $rootNode = "<{$root}/>";
    

    【讨论】:

    • 感谢您的回答,但如果我理解正确,我需要在发送数组以发挥作用时添加类似的内容:$var="&lt;root/&gt;"; $ArrayToXml-&gt;toXml($library,'data',$var);
    • 您需要知道您的 XML 文件会是什么样子,并相应地修复对 simplexml_load_string 的调用。 $rootNodeName 似乎是一个不错的候选人,不是吗?
    • 例如我想看起来像我上面的测试代码。书将是根节点,然后是名称..如示例..但是如何发送数据以使此代码工作
    • 类似的东西:simplexml_load_string("&lt;".key($data)."/&gt;");
    • 我使用上面的两个文件和我给你的修复程序让它工作:pastie.org/private/rmumpjgfyppqolzurfna
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多