Ways to add CDATA tags when PHP generates XML

Source: Internet
Author: User
Tags cdata

The example in this article describes how PHP adds a CDATA tag when it generates XML. Share to everyone for your reference. The implementation methods are as follows:

In fact, PHP to generate XML when adding CDATA tag method is very simple, because it is a in the XML can store a variety of content in the tag, the following an example of a sample to help you.

There's a code that has the truth, put the codes on, everybody don't put <! [cdata[$text]]> as a prefix, in fact it can be a label.

The specific code is as follows:

code is as follows: &lt;?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-&gt;createelement ("toppings");


$dom-&gt;appendchild ($root);


//Create child element


$item = $dom-&gt;createelement ("item");


$root-&gt;appendchild ($item);


//Create text node


$text = $dom-&gt;createtextnode ("pepperoni");


$item-&gt;appendchild ($text);


//Create attribute node


$price = $dom-&gt;createattribute ("price");


$item-&gt;appendchild ($price);


//Create attribute value node


$priceValue = $dom-&gt;createtextnode ("4");


$price-&gt;appendchild ($priceValue);


//Create CDATA section


$cdata = $dom-&gt;createcdatasection ("Customer requests that pizza is sliced into square pieces");


$root-&gt;appendchild ($cdata);


//Create PI


$pi = $dom-&gt;createprocessinginstruction ("pizza", "bake ()");


$root-&gt;appendchild ($PI);


//Save and Display tree


echo $dom-&gt;savexml ();


?&gt;

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.