Php adds the CDATA label when generating xml, and the phpxmlcdata label

Source: Internet
Author: User

Php adds the CDATA label when generating xml, and the phpxmlcdata label

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

I hope this article will help you with php programming.


How does php extract the content of CDATA in xml?

You can try to use simpleXml to parse this file, which is much simpler than EXpat.

Who used Xstream to generate xml with cdata labels to parse xml with cdata labels?

In XML, CDATA indicates that internal data is not escaped, that is, characters such as <,> are not interpreted.
When parsing such xml, the interpreter reads its content as is without any special processing.
 

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.