我正在尝试创建xml文件,
向子对象添加属性时遇到问题。
$this->myXML = new SimpleXMLElement('<start></start>');
$a[] = $this->myXML->addChild('parent');
$y = $a[0];
$y->addAttribute('type', 'documentary');
$y->addAttribute('type2', 'documentary');
$y[0] =11;
$a[1] = $a[0];
$s = $a[1]->children();
$s[-1]['sample'] = 123;
$s[1] = 1212;
输出
<?xml version="1.0" standalone="yes"?>
<start>
<parent type="documentary" type2="documentary">11</parent>
<parent sample="123">1212</parent>
</start>
这对于一个属性
是可以的$s = $a[1]->children();
$s[-1]['sample'] = 123;
$s[-1]['sample2'] = 123;
$s[1] = 1212;
输出
<?xml version="1.0" standalone="yes"?>
<start>
<parent type="documentary" type2="documentary">11</parent
<parent sample="123">1212</parent>
<parent sample2="123"/>
</start>
我尝试这样的事情:
$s = $a[1]->children();
$s->parent = 12;
$s->addAttribute('xy',123);
$s->addAttribute('x',1234);
$w = clone $s->parent;
unset($s->parent);
$s[1] = $w[0];
输出
<?xml version="1.0" standalone="yes"?>
<start>
<parent type="documentary" type2="documentary">11</parent>
<parent>12</parent>
</start>
可以向child添加属性吗?