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:
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:
- Create a new document object and load the XML
- Get all
Destination
elements by tag name and iterate over the list
- Iterate over all child nodes of each
Destination
element
- Check if the node type is
XML_CDATA_SECTION_NODE
- 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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…