Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add CDATA to xml node

Tags:

php

xml

This php code works correctly, but how do I add CDATA to content node?

 <?php
 $xml = new DomDocument("1.0", "UTF-8");
 $xml->load('xmldata.xml');

 $title    = $_POST['title'];
 $avtor    = $_POST['avtor'];
 $date     = $_POST['date'];
 $category = $_POST['category'];
 $content  = $_POST['content'];

$rootTag = $xml->getElementsByTagName("root")->item(0);

   $postingTag = $xml->createElement("posting");

     $titleTag    = $xml->createElement("title",    $title);
     $avtorTag    = $xml->createElement("avtor",    $avtor);
     $dateTag     = $xml->createElement("date",     $date);
     $categoryTag = $xml->createElement("category", $category);
     $contentTag  = $xml->createElement("content",  $content);

     $postingTag->appendChild($titleTag);
     $postingTag->appendChild($avtorTag);
     $postingTag->appendChild($dateTag);
     $postingTag->appendChild($categoryTag);
     $postingTag->appendChild($contentTag);

   $rootTag->appendChild($postingTag);

$xml->formatOutput = true;
$xml->save('xmldata.xml');
like image 921
Mihail Novak Avatar asked Jul 06 '15 07:07

Mihail Novak


People also ask

How do I add CDATA to XML?

CDATA sections can appear inside element content and allow < and & character literals to appear. A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &.

Can we use CDATA in XML attribute?

No, The markup denoting a CDATA Section is not permitted as the value of an attribute. According to the specification, this prohibition is indirect rather than direct. The spec says that the Attribute value must not have an open angle bracket.

Should I use CDATA in XML?

You should almost never need to use CDATA Sections. The CDATA mechanism was designed to let an author quote fragments of text containing markup characters (the open-angle-bracket and the ampersand), for example when documenting XML (this FAQ uses CDATA Sections quite a lot, for obvious reasons).

Is CDATA deprecated?

Note: CDATA is now deprecated. Do not use. The CDATA Section interface is used within XML for including extended portions of text. This text is unescaped text, like < and & symbols.


3 Answers

DOM separates node create and append. You create the node using a method of the document and append it using methods of the parent node.

Here is an example:

$document = new DOMDocument();
$root = $document->appendChild(
  $document->createElement('element-name')
);
$root->appendChild(
  $document->createCDATASection('one')
);
$root->appendChild(
  $document->createComment('two')
);
$root->appendChild(
  $document->createTextNode('three')
);

echo $document->saveXml();

Output:

<?xml version="1.0"?>
<element-name><![CDATA[one]]><!--two-->three</element-name>

DOMNode::appendChild() and similar methods return the appended node, so you can combine them with the DOMDocument::create*() call.

like image 102
ThW Avatar answered Oct 29 '22 12:10

ThW


CDATA or a CDATA section?

$cdata = 'This is my character data!';

For the first use the second parameter of createElement('tagname', 'cdata') - hey you already do it here:

$contentTag  = $xml->createElement("content",  $content);
                                               ^^^^^^^^

for the second createCDATASection() and append it as child to the created element:

$contentTag  = $xml->createElement("content",  $content);
$contentTag->appendChild($xml->createCDATASection($cdata);
like image 32
hakre Avatar answered Oct 29 '22 13:10

hakre


Or something like this should work

  $name          = $dom->createElement('name');
  $cdataname     = $dom->createCDATASection('Your CDATA text');

  $name->appendChild($cdataname);

  // whatever node you have here
  $node->appendChild($name);

It will produce this:

  <product>
   <name>
     <![CDATA[Your CDATA text]]>
   </name>
  </product>
like image 1
Stavros Avatar answered Oct 29 '22 12:10

Stavros