簡體   English   中英

PHP:如何使用SimpleXMLElement將屬性添加到更深的子注釋中

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

如何使用SimpleXMLElement將屬性添加到更深的子注釋中? 例如,

$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());

我明白了,

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

但實際上我想要,

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

可能嗎?

<?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