使用PHP设置XML属性


Setting XML attribute with PHP

我正在尝试使用SimpleXMLElement,用PHP创建具有特定布局的.xml文件。目前,我正在努力为元素设置属性。

到目前为止我尝试了什么:

<?php
$att = 'name="tc1"';
$xml = new SimpleXMLElement('<configuration/>');
$track = $xml->addChild('testcase');
$track->attributes()->$att;
$track->addChild('pfad', "none");
$track->addChild('aktiv', "false");
$track->addChild('file', "none");
$track->addChild('wert', "none");

Header('Content-type: text/xml');
print($xml->asXML());
?>

我不明白为什么它只显示:
(XML输出:)

<configuration>
  <testcase>
    <pfad>none</pfad>
    <aktiv>false</aktiv>
    <file>none</file>
    <wert>none</wert>
 </testcase>
</configuration>

而不是:

...
<testcase name="tc1">
...

你能帮我正确设置属性吗?

$track->addAttribute('name', 'tc1');