简体   繁体   English

PHP:如何使用SimpleXMLElement将属性添加到更深的子注释中

[英]PHP: How to add attributes to the deeper child note with SimpleXMLElement

How can I add attributes to the deeper child note with SimpleXMLElement ? 如何使用SimpleXMLElement将属性添加到更深的子注释中? for instance, 例如,

$xml = new SimpleXMLElement('<xml/>');

$response = $xml->addChild('response');
$response->addChild('error');
$response->addAttribute('elementid', 100);
$response->addAttribute('message', 'You must not leave this field empty!');


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

I get this, 我明白了,

<xml>
<response elementid="100" message="Key name - You must not leave this field empty!">
<error />
</response>
</xml>

But actually I want, 但实际上我想要,

<xml>
<response>
<error elementid="100" message="Key name - You must not leave this field empty!" />
</response>
</xml>

Is it possible? 可能吗?

<?php
$xml = new SimpleXMLElement('<xml/>');

$response   = $xml->addChild('response');
$error      = $response->addChild('error');
$error->addAttribute('elementid', 100);
$error->addAttribute('message', 'You must not leave this field empty!');

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM