Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Working with PHP DOM is fairly straightforward, and is very similar to Javascript's DOM.</p> <p>Here are the important classes:</p> <ul> <li><a href="http://www.php.net/manual/en/class.domnode.php" rel="noreferrer"><strong>DOMNode</strong></a> &mdash; The base class for anything that can be traversed inside an XML/HTML document, including text nodes, comment nodes, and <strong>CDATA nodes</strong></li> <li><a href="http://www.php.net/manual/en/class.domelement.php" rel="noreferrer"><strong>DOMElement</strong></a> &mdash; The base class for <strong>tags</strong>.</li> <li><a href="http://www.php.net/manual/en/class.domdocument.php" rel="noreferrer"><strong>DOMDocument</strong></a> &mdash; The base class for documents. Contains the methods to load/save XML, as well as normal DOM document methods (see below).</li> </ul> <p>There are a few staple methods and properties:</p> <ul> <li><a href="http://www.php.net/manual/en/domdocument.load.php" rel="noreferrer"><code>DOMDocument-&gt;load()</code></a> &mdash; After creating a new <code>DOMDocument</code>, use this method on that object to load from a file.</li> <li><a href="http://www.php.net/manual/en/domdocument.getelementsbytagname.php" rel="noreferrer"><code>DOMDocument-&gt;getElementsByTagName()</code></a> &mdash; this method returns a node list of all elements in the document with the given tag name. Then you can iterate (<code>foreach</code>) on this list.</li> <li><a href="http://www.php.net/manual/en/class.domnode.php#domnode.props.childnodes" rel="noreferrer"><code>DOMNode-&gt;childNodes</code></a> &mdash; A node list of all children of a node. (Remember, a <strong>CDATA</strong> section is a node!)</li> <li><a href="http://www.php.net/manual/en/class.domnode.php#domnode.props.nodetype" rel="noreferrer"><code>DOMNode-&gt;nodeType</code></a> &mdash; Get the <a href="http://www.php.net/manual/en/dom.constants.php" rel="noreferrer">type</a> of a node. CDATA nodes have type <strong>XML_CDATA_SECTION_NODE</strong>, which is a constant with the value <strong>4</strong>.</li> <li><a href="http://www.php.net/manual/en/class.domnode.php#domnode.props.textcontent" rel="noreferrer"><code>DOMNode-&gt;textContent</code></a> &mdash; get the text content of any node.</li> </ul> <p><strong>Note</strong>: Your CDATA sections are malformed. I don't know why there is an extra <code>]]</code> in the first one, or an unclosed CDATA section at the end of the line, but I think it should simply be:</p> <pre><code>&lt;![CDATA[Aghia Paraskevi, Skiatos, Greece]]&gt; </code></pre> <p><strong>Putting this all together we:</strong></p> <ol> <li>Create a new document object and load the XML</li> <li>Get all <code>Destination</code> elements by tag name and iterate over the list</li> <li>Iterate over all child nodes of each <code>Destination</code> element</li> <li>Check if the node type is <code>XML_CDATA_SECTION_NODE</code></li> <li>If it is, <code>echo</code> the <code>textContent</code> of that node.</li> </ol> <p><strong>Code:</strong></p> <pre><code>$doc = new DOMDocument(); $doc-&gt;load('test.xml'); $destinations = $doc-&gt;getElementsByTagName("Destination"); foreach ($destinations as $destination) { foreach($destination-&gt;childNodes as $child) { if ($child-&gt;nodeType == XML_CDATA_SECTION_NODE) { echo $child-&gt;textContent . "&lt;br/&gt;"; } } } </code></pre> <p><strong>Result:</strong></p> <blockquote> <p>Aghia Paraskevi, Skiatos, Greece<br> Amettla, Spain<br> Amoliani, Greece<br> Boblingen, Germany</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload