in response to tildy at pr dot hu
my preferred way is (in example to gather country data from an iso 3166 xml flie)
$countries = new DOMDocument();
$countries->load("./lib/iso_3166.xml");
$countriesList = $countries->getElementsByTagName("ISO_3166-1_Entry");
foreach($countriesList as $country) {
$values = $country->getElementsByTagName("*");
foreach($values as $node) {
echo $node->nodeName."=".$node->nodeValue;
}
}
DOMDocument->getElementsByTagName()
(No version information available, might be only in CVS)
DOMDocument->getElementsByTagName() — Searches for all elements with given tag name
Popis
DOMDocument
DOMNodeList getElementsByTagName
( string $name
)
This function returns a new instance of class DOMNodeList containing the elements with a given tag name.
Parametre
- name
-
The name of the tag to match on. The special value * matches all tags.
Vrátené hodnoty
A new DOMNodeList object containing all the matched elements.
DOMDocument->getElementsByTagName()
jason at shaped dot ca
11-Feb-2008 04:59
11-Feb-2008 04:59
tildy at pr dot hu
13-Dec-2007 04:51
13-Dec-2007 04:51
If you want to list the nodename and value of one item(node) this is an example:
$itemnodes = $doc->getElementsByTagName( "item" );
$nodes = $itemnodes->item(0)->getElementsByTagName( "*" );
for ( $i = 0; $i < $nodes->length; $i++ ) {
print "nodename=".$nodes->item( $i )->nodeName;
print "\t";
print "nodevalue : ".$nodes->item( $i )->nodeValue;
print "\r\n";
}
It will be list all children name and value of item.
Francois Hill
30-Jul-2007 07:36
30-Jul-2007 07:36
Careful : getElementsByTagName will yield all elements with the given tag name under the present node, at any sub-level (i.e. among child nodes and all other descendant nodes)
Finding values of a node
14-Mar-2007 03:01
14-Mar-2007 03:01
I don't know if this is that obvious but it was not for me, so in addition to gem at rellim dot com's posting:
adding
<?php
echo $param -> nodeValue.'<br>';
?>
to the loop will output
value1
value2
value3
gem at rellim dot com
29-Sep-2004 04:20
29-Sep-2004 04:20
Here is an example of getElementsByTagName():
<?php
$xml =<<<EOT
<?xml version="1.0"?>
<config>
<section id="section1">
<param name="param1">value1</param>
<param name="param2">value2</param>
</section>
<section id="section2">
<param name="param3">value3</param>
</section>
</config>
EOT;
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$params = $dom->getElementsByTagName('param');
foreach ($params as $param) {
echo $param -> getAttribute('name').'<br>';
}
?>
Expected result:
--------------
param1
param2
param3
