LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 07-12-2011, 01:52 AM   #1
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: 0
How to traverse through the XML nodes?


Dear friends of linuxquestions.org;
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 :
<Root>
<name></name>
<surname></surname>
<ID></ID>
<department></department>
<rank></rank>
<telNo></telNo>
<deckNo></deckNo>
<mobileNo secrecy="c"></mobileNo>
<homeTelNo secrecy="c"></homeTelNo>
</Root>

Here are the php code of mine:

Code:
$in_name = $_POST["name"]; 
$in_surname = $_POST["surname"];
$in_department = $_POST["department"];
 
$doc = new DOMDocument();
$doc->load('http://localhost/xampp/tsbs_yellow_pages/run/1/databank.xml');
$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++) 
			{
				echo("<p>$parent_nodes->nodeValues</p>\n");
			}
			

		}
		
	}
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, 01:25 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

This is a great tutorial:

http://www.ibm.com/developerworks/xm...ary/x-xmlphp1/

http://www.ibm.com/developerworks/xm...ary/x-xmlphp2/

http://www.ibm.com/developerworks/xm...ary/x-xmlphp3/

From Chap 2, I believe this answers your question:
Code:
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.
  


Reply

Tags
php


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 06:06 AM
Modifying Specific Child Nodes In XML using Shell Script senthilmuthiah Linux - Newbie 1 04-20-2009 05:38 AM
How actually packet traverse in IPTables priyadarshan Linux - Networking 3 02-10-2009 12:26 PM
configure: error: could not find DocBook XML DTD V4.1.2 in XML catalog Fadoksi Linux - Software 1 07-16-2006 07:41 AM
How to remove single-line XML nodes from files dfrechet Programming 1 10-11-2005 03:00 PM


All times are GMT -5. The time now is 04:29 AM.

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