I have to create an xml string to be sent in a post request. I am using SimpleXMLElement
library of PHP
to create xml for the same.
I want to create a nested xml with one of the tag as self closing.
So Far I have created tags with elements having attributes and children as follows
$envelop = new SimpleXMLElement("<Envelope></Envelope>");
$body = $envelop->addChild('Body');
.
.
.
.
$envelop->asXML(); // Gives me proper XML
What I want to achieve using SimpleXMLElement
element now is a self closing tag.
<Envelope>
<Body>
<ExportList>
<LIST_ID>234234</LIST_ID>
<EXPORT_TYPE>ALL</EXPORT_TYPE>
<EXPORT_FORMAT>CSV</EXPORT_FORMAT>
<ADD_TO_STORED_FILES/> < -- Target Tag -->
<DATE_START>07/25/2003 12:12:11</DATE_START>
<DATE_END>09/30/2005 14:14:11</DATE_END>
</ExportList>
</Body>
</Envelope>
I tried searching for solutions and docs. But I was not able to find any any solutions for this one. Please if anybody could help in achieving the above task.
Also FYI I've got solutions using DOMElement
, but I am not interested in using that, cause then I will need to re write my whole application. Please let me know if this can be achieved using SimpleXMLElement
.
While I was running by head around so impatiently for the solution, in the end it was really simple. It was mentioned no where, I just tried adding a tag with no second parameter to a parent tag. Simple(XMLElemet) as that :P
Below is the code:
$envelop = new SimpleXMLElement("<Envelope></Envelope>");
$body = $envelop->addChild('Body');
$exportList = $body->addChild('ExportList');
$exportList->addChild('LIST_ID', 234234);
$exportList->addChild('ADD_TO_STORED_FILES'); // passing nothing
.
.
.
$envelop->asXML();
Conclusion : So basically if its a leaf node with no data, it will be created as self closing.
Now I need to check how to create a leaf node with non self closing tag. That will be another question. But later :)
For now I am Done!