'How to get values inside <![CDATA[values]] > using php DOM?

How can i get values inside <![CDATA[values]] > using php DOM. This is few code from my xml.

     <Destinations>

        <Destination>
            <![CDATA[Aghia Paraskevi, Skiatos, Greece]]>
            <CountryCode>GR</CountryCode>
        </Destination>

        <Destination>
            <![CDATA[Amettla, Spain]]>
            <CountryCode>ES</CountryCode>
        </Destination>

        <Destination>
            <![CDATA[Amoliani, Greece]]>
            <CountryCode>GR</CountryCode>
        </Destination>

        <Destination>
            <![CDATA[Boblingen,  Germany]]>
            <CountryCode>DE</CountryCode>
        </Destination>

  </Destinations>


Solution 1:[1]

Working with PHP DOM is fairly straightforward, and is very similar to Javascript's DOM.

Here are the important classes:

  • DOMNode — The base class for anything that can be traversed inside an XML/HTML document, including text nodes, comment nodes, and CDATA nodes
  • DOMElement — The base class for tags.
  • DOMDocument — The base class for documents. Contains the methods to load/save XML, as well as normal DOM document methods (see below).

There are a few staple methods and properties:

  • DOMDocument->load() — After creating a new DOMDocument, use this method on that object to load from a file.
  • DOMDocument->getElementsByTagName() — this method returns a node list of all elements in the document with the given tag name. Then you can iterate (foreach) on this list.
  • DOMNode->childNodes — A node list of all children of a node. (Remember, a CDATA section is a node!)
  • DOMNode->nodeType — Get the type of a node. CDATA nodes have type XML_CDATA_SECTION_NODE, which is a constant with the value 4.
  • DOMNode->textContent — get the text content of any node.

Note: Your CDATA sections are malformed. I don't know why there is an extra ]] in the first one, or an unclosed CDATA section at the end of the line, but I think it should simply be:

<![CDATA[Aghia Paraskevi, Skiatos, Greece]]>

Putting this all together we:

  1. Create a new document object and load the XML
  2. Get all Destination elements by tag name and iterate over the list
  3. Iterate over all child nodes of each Destination element
  4. Check if the node type is XML_CDATA_SECTION_NODE
  5. If it is, echo the textContent of that node.

Code:

$doc = new DOMDocument();
$doc->load('test.xml');
$destinations = $doc->getElementsByTagName("Destination");
foreach ($destinations as $destination) {
    foreach($destination->childNodes as $child) {
        if ($child->nodeType == XML_CDATA_SECTION_NODE) {
            echo $child->textContent . "<br/>";
        }
    }
}

Result:

Aghia Paraskevi, Skiatos, Greece
Amettla, Spain
Amoliani, Greece
Boblingen, Germany

Solution 2:[2]

Use this:

$parseFile = simplexml_load_file($myXML,'SimpleXMLElement', LIBXML_NOCDATA)

and next :

foreach ($parseFile->yourNode as $node ){
etc...
}

Solution 3:[3]

Best and easy way

$xml = simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA);
$xmlJson = json_encode($xml);
$xmlArr = json_decode($xmlJson, 1); // Returns associative array

Solution 4:[4]

Use replace CDATA before parsing PHP DOM element after that you can get the innerXml or innerHtml:

str_replace(array('<\![CDATA[',']]>'), '', $xml);

Solution 5:[5]

I use following code. Its not only read all xml data with

<![CDATA[values]] > 

but also convert xml object to php associative array. So we can apply loop on the data.

$xml_file_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA),true), true);

Hope this will work for you.

Solution 6:[6]

function inBetweenOf(string $here, string $there, string $content) : string {
    $left_over = strlen(substr($content, strpos($content, $there)));
    return substr($content, strpos($content, $here) + strlen($here), -$left_over);
}

Iterate over "Destination" tags and then call inBetweenOf on each iteration.

$doc = inBetweenOf('<![CDATA[', ']]>', $xml);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Grzegorz Brz?czyszczykiewicz
Solution 3 Aniket Singh
Solution 4 Tisho
Solution 5 Adnan Ahmad
Solution 6