How to add the CDATA tag when php generates xml

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.