note 109860 added to domdocument.getelementsbytagname
| From: | yaakov dot moddel at gmail dot com | Date: | Tue, 28 Aug 2012 22:47:14 +0000 |
| Subject: | note 109860 added to domdocument.getelementsbytagname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190251@lists.php.net to get a copy of this message | ||
This is a very simplistic way to traverse xml nodes and childnodes using the DOMDocument class
<?php
$xml ='<?xml version="1.0" encoding="utf-8"?>
<root>
<Parent>
<child>child 1</child>
<child>child 2</child>
<child>child 3</child>
<subParent>
<Grandchild>Grandchild 1</Grandchild>
<Grandchild>Grandchild 2</Grandchild>
<Grandchild>Grandchild 3</Grandchild>
</subParent>
</Parent>
<Parent>
<child>child 4</child>
<child>child 5</child>
<child>child 6</child>
<subParent>
<Grandchild>Grandchild 4</Grandchild>
<Grandchild>Grandchild 5</Grandchild>
<Grandchild>Grandchild 6</Grandchild>
</subParent>
</Parent>
</root>';
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml);
$i=0;
while(is_object($finance = $doc->getElementsByTagName("Parent")->item($i)))
{
foreach($finance->childNodes as $nodename)
{
if($nodename->nodeName=='subParent')
{
foreach($nodename->childNodes as $subNodes)
{
echo $subNodes->nodeName." -
".$subNodes->nodeValue."<br>";
}
}
else
{
echo $nodename->nodeName." - ".$nodename->nodeValue."<br>";
}
}
$i++;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 67.83.25.171
----
Manual Page -- http://www.php.net/manual/en/domdocument.getelementsbytagname.php
Edit -- https://master.php.net/note/edit/109860
Del: integrated -- https://master.php.net/note/delete/109860/integrated
Del: useless -- https://master.php.net/note/delete/109860/useless
Del: bad code -- https://master.php.net/note/delete/109860/bad+code
Del: spam -- https://master.php.net/note/delete/109860/spam
Del: non-english -- https://master.php.net/note/delete/109860/non-english
Del: in docs -- https://master.php.net/note/delete/109860/in+docs
Del: other reasons-- https://master.php.net/note/delete/109860
Reject -- https://master.php.net/note/reject/109860
Search -- https://master.php.net/manage/user-notes.php