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>
Use an XML parser. Show activity on this post. you should use simple_xml and xpath if you need to extract a complex set of data.
A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.
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 &.
The only sequence which is not allowed within a CDATA section is the closing sequence of a CDATA section itself, ]]> . Note: CDATA sections should not be used within HTML they are considered as comments and not displayed.
Working with PHP DOM is fairly straightforward, and is very similar to Javascript's DOM.
Here are the important classes:
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:
Destination
elements by tag name and iterate over the listDestination
elementXML_CDATA_SECTION_NODE
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
Use this:
$parseFile = simplexml_load_file($myXML,'SimpleXMLElement', LIBXML_NOCDATA)
and next :
foreach ($parseFile->yourNode as $node ){ etc... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With