Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-12-2011, 12:52 AM   #1
Registered: Mar 2010
Location: Turkey
Posts: 35

Rep: Reputation: 0
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.
Old 07-12-2011, 12:25 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
1 members found this post helpful.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to traverse a directory with certain permissions? wringer Linux - Newbie 1 06-30-2011 05:06 AM
Modifying Specific Child Nodes In XML using Shell Script senthilmuthiah Linux - Newbie 1 04-20-2009 04:38 AM
How actually packet traverse in IPTables priyadarshan Linux - Networking 3 02-10-2009 11:26 AM
configure: error: could not find DocBook XML DTD V4.1.2 in XML catalog Fadoksi Linux - Software 1 07-16-2006 06:41 AM
How to remove single-line XML nodes from files dfrechet Programming 1 10-11-2005 02:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration