note 85292 deleted from function.domelement-get-elements-by-tagname by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:43:53 +0000 |
| Subject: | note 85292 deleted from function.domelement-get-elements-by-tagname by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207433@lists.php.net to get a copy of this message | ||
Note Submitter: mikhail dot nemtsev at gmail dot com
----
This example uses get_elements_by_tagname() and child_nodes() to output the contents of the xml
file. The sample xml file and php code are below.
<?xml version="1.0" encoding="iso-8859-1"?>
<dictionary>
<entry>
<word>Hello</word>
<meaning>used to express a greeting</meaning>
<example>Hello World!</example>
</entry>
</dictionary>
<?php
$xml_path = "/some/dir/dictionary.xml";
if (!$dom = domxml_open_file($xml_path)) {
//the above requires absolube path
exit("Could not access the XML source.");
}
$root = $dom->document_element(); //obtain the root
$array_entry = $root->get_elements_by_tagname("entry"); //gets all <entry>
DomElems
foreach ($array_entry as $entry_dom_elem) {
$childnodes = $entry_dom_elem->child_nodes();
foreach ($childnodes as $child) { //cycle thru <word>, <meaning>, <example>
echo "node name: ".$child->tagname() . "; contains: " .
$child->get_content()."<br>";
}
}
?>
The above code outputs:
node name: word; contains: Hello
node name: meaning; contains: used to express a greeting
node name: example; contains: Hello World!