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;
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.