【发布时间】: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 传递的,因此不需要& -
@Xenos 那个参数在那里,我写的代码我写了一个不能缺少的个人信息的原因
-
@Xenos 也感谢你不知道我这么笨的笔记
-
这是真实的代码吗?
&xml一定是个错误。 -
& 不再存在,&$xml 是一个引用,但正如上面的人所说,对象总是通过引用传递,所以不,它没有错误,现在它消失了,因为它已经是一个引用:php.net/manual/en/language.references.php
标签: php xml domdocument