简体   繁体   中英

write element with attribute xml php

I am writing xml using php. I want to write attribute with element. My code is as below

$writer = new XMLWriter();  
$writer->openURI('php://output');   
$writer->startDocument('1.0','UTF-8');   
$writer->setIndent(4);   
$writer->startElement('Response');
$writer->writeElement("Dial","+111111111");
$writer->endElement();
$writer->endDocument();   
$writer->flush();

This produces this output

<response>
<dial>+111111</dial>
</response>

But I want something like this

<response>
<dial action="myaction">+111111</dial>
</response>

I tried this

$writer->writeAttribute('action', 'myaction');

But this added only with startElement not with writeElement. Thanks

Instead line $writer->writeElement("Dial","+111111111"); you should write the element in some steps to add attribute

$writer->startElement("Dial");
$writer->writeAttribute('action', "myaction");
$writer->text("+111111111");
$writer->endElement();

demo on eval.in

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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