【问题标题】:selfwritten xml generator doesnt listen to the xml output自写的 xml 生成器不听 xml 输出
【发布时间】:2017-02-06 05:20:36
【问题描述】:

我有一个包含以下内容的页面:

$xml = new DOMDocument('1.0', 'UTF-8');
// Set de format van het xml document
$xml->formatOutput = true;
$test = $xml->createElement('test');
$xml->appendChild($test);
$test->createElement('name','pierre');

XMLController::CreateTestXML($xml);

我写了两个函数。

1.) CreateMultipleElements()
2.) CreateTestXML()

CreateTestXML() 函数包含:
此函数旨在向 xml 添加一些内容,它确实:

public static function CreateTestXML(&xml){
    $objectArray = self::CreateMultipleElements(
            $xml, 
            array(
                "Persoon" => null,
                "Paard" => "Wit"
            ),
            $parent = null, 
            $attributes = array(
                "Persoon" => array(
                    "Type" => "NPS"
                )
            )
        );
}

然后我们有了实际创建节点和正常工作的元素的函数:

public static function CreateMultipleElements(&$xml,$objectNameValue,$parent = null, $attributes = null){
        $xmlObjecten = array();
        foreach($objectNameValue as $objectName => $nodeValue){
            $object = $xml->createElement($objectName,$nodeValue);
            if(isset($attributes[$objectName])){
                foreach($attributes[$objectName] as $key => $value){
                    $object->setAttribute($key,$value);
                }
            }
            if($parent === null){
                $xmlObjecten[$objectName] = $xml->appendChild($object);
            }
            else{
                $xmlObjecten[$objectName] = $parent->appendChild($object);
            }
        }
        return $xmlObjecten;
    }

xml 输出如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <name>pierre</name>
</test>
<persoon type="NPS"></persoon><paard>wit</paard>

所以生成的部分似乎忽略了格式输出,请帮助

【问题讨论】:

  • 您正在调用没有参数的静态方法,而他们需要 $xml 作为他们的第一个参数。请注意,对象是通过 ref 传递的,因此不需要 &amp;
  • @Xenos 那个参数在那里,我写的代码我写了一个不能缺少的个人信息的原因
  • @Xenos 也感谢你不知道我这么笨的笔记
  • 这是真实的代码吗? &amp;xml 一定是个错误。
  • & 不再存在,&$xml 是一个引用,但正如上面的人所说,对象总是通过引用传递,所以不,它没有错误,现在它消失了,因为它已经是一个引用:php.net/manual/en/language.references.php

标签: php xml domdocument


【解决方案1】:

您的 XML 无效:它不能有多个根节点。您必须只有一个节点(即&lt;test&gt;)。

XML 中多个根节点的行为肯定是未定义的,因此是“部分格式化的输出”。

【讨论】:

  • 编辑:在我们的真实事物中,我们有一个根。问题是它不采用输出布局
  • @RoyStijsiger 然后显示“你的真品”(我会后悔这个评论...)
  • 它只是一个根,在此功能中使用内部关闭
猜你喜欢
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
相关资源
最近更新 更多