-   Programming (
-   -   How to traverse through the XML nodes? (

gozlemci 07-12-2011 12:52 AM

How to traverse through the XML nodes?
Dear friends of;
I'm struggling with a very irritating problem:
I have a xml file and I read it within a php file with XML dom.

This is the node structure of my xml file :
<mobileNo secrecy="c"></mobileNo>
<homeTelNo secrecy="c"></homeTelNo>

Here are the php code of mine:


$in_name = $_POST["name"];
$in_surname = $_POST["surname"];
$in_department = $_POST["department"];
$doc = new DOMDocument();
$xPath = new DOMXPath($doc);

if($in_name != '')
                $nodeList = $xPath->query('//User/name');
                $phase = trim(($in_name));
                $length_of_phase = strlen($phase);

        foreach($nodeList as $node)
                if(  strncasecmp( trim($node->nodeValue),$phase,$length_of_phase) == 0)
                        $parent_nodes = $node->parentNode;
                        //Here is the point
                        for($i = 1; $i <= 4; $i++)


In the comment line, I've got the parent node of the current node. Parent node is "User" node. I would like to obtain the children of this parent node, say "name","surname " etc. How can I do this? Is there any function such as : "$parent_nodes->bringChildren('name')". I could not find it.

Thanks in advance.

paulsm4 07-12-2011 12:25 PM

Hi -

This is a great tutorial:

From Chap 2, I believe this answers your question:

xpath = new DOMXPath($doc);

// We start from the root element
$query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';

$entries = $xpath->query($query);

foreach ($entries as $entry) {
  echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
        " by {$entry->previousSibling->nodeValue}\n";

'Hope that helps .. PSM

All times are GMT -5. The time now is 02:41 PM.