php生成xml时添加CDATA标签方法非常的简单,因为是一个在xml中可以存储各种内容的标签了,下面整理了一个例子希望对各位有帮助.
贴上代码留住伤疤,不要把<![CDATA[ $text]]>当成前后缀,其实它可以是标签,代码如下:
- <?php
-
- $dom = new DOMDocument("1.0");
-
-
- header("Content-Type: text/plain");
-
- $root = $dom->createElement("toppings");
- $dom->appendChild($root);
-
- $item = $dom->createElement("item");
- $root->appendChild($item);
-
- $text = $dom->createTextNode("pepperoni");
- $item->appendChild($text);
-
- $price = $dom->createAttribute("price");
- $item->appendChild($price);
-
- $priceValue = $dom->createTextNode("4");
- $price->appendChild($priceValue);
-
- $cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
- $root->appendChild($cdata);
-
- $pi = $dom->createProcessingInstruction("pizza", "bake()");
- $root->appendChild($pi);
-
- echo $dom->saveXML();
- ?>
|