Reputation: 962
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
Upvotes: 2
Views: 2281
Reputation: 26153
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();
Upvotes: 8