How to add the CDATA tag when php generates xml
This example describes how to add the CDATA tag when php generates xml. Share it with you for your reference. The specific implementation method is as follows:
In fact, the method of adding CDATA labels when php generates xml is very simple, because it is a tag that can store various content in xml. The following is an example to help you.
If there is code, there is truth, and the code is pasted on, do not <! [CDATA [$ text]> as the prefix suffix, it can actually be a tag.
The Code is as follows:
The Code is as follows:
<? Php
$ Dom = new DOMDocument ("1.0 ");
// Display document in browser as plain text
// For readability purposes
Header ("Content-Type: text/plain ");
// Create root element
$ Root = $ dom-> createElement ("toppings ");
$ Dom-> appendChild ($ root );
// Create child element
$ Item = $ dom-> createElement ("item ");
$ Root-> appendChild ($ item );
// Create text node
$ Text = $ dom-> createTextNode ("pepperoni ");
$ Item-> appendChild ($ text );
// Create attribute node
$ Price = $ dom-> createAttribute ("price ");
$ Item-> appendChild ($ price );
// Create attribute value node
$ PriceValue = $ dom-> createTextNode ("4 ");
$ Price-> appendChild ($ priceValue );
// Create CDATA section
$ Cdata = $ dom-> createCDATASection ("Customer requests that pizza be sliced into 16 square pieces ");
$ Root-> appendChild ($ cdata );
// Create PI
$ Pi = $ dom-> createProcessingInstruction ("pizza", "bake ()");
$ Root-> appendChild ($ pi );
// Save and display tree
Echo $ dom-> saveXML ();
?>