Populating an array through traversed XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattward1979
Forum Newbie
Posts: 5
Joined: Fri Mar 04, 2011 10:11 am

Populating an array through traversed XML

Post by mattward1979 »

hello all,

I have been struggling with this for hours now so really hope you can help.

I have an XML file which is created through aggregation of results from a MySQL DB and a SqlServer DB, and to create pagination on my page, I need to traverse this XML document and create a NEW XML file with just 5 results.

The bit I am having trouble with is extracting the XML Child Node text fields from the original document....

this is a snippet of the XML

<?xml version="1.0"?>
<Traders>
<Trader>
<businessName>Plumbers Inc</businessName>
<phone>02083113604</phone>
<emPhone>08005554561</emPhone>
<businessEmail>123@123.com</businessEmail>
<area>Lewisham</area>
</Trader>
<Trader>
<businessName>The Trashmen</businessName>
<phone>02083113604</phone>
<emPhone>08005554561</emPhone>
<businessEmail>123@123.com</businessEmail>
<area>Lewisham</area>
</Trader>
</Traders>


and this is the PHP that I have tried without success :

Code: Select all

$records = $newDoc->getElementsByTagName('Trader');
	# Calculate the first and last records
	$pageLength = 5;
	$first = ( isset($_GET['next']) ) ? $_GET['next'] : 0;
	$last = ( $records->length - $first < $pageLength ) ? $records->length : $first + $pageLength;

	$RecordsXML = array();
	for ($i=$first; $i<$last; $i++) 
			{
				
				$RecordsXML [] = array(
				'businessName' => $records->item($i)->firstChild->textContent,
				'phone' =>  $records->item($i)->firstChild->nextSibling->textContent,
				'mobPhone' =>  $records->item($i)->firstChild->nextSibling->textContent,
				'emPhone' => $records->item($i)->firstChild->nextSibling->textContent,
				'businessEmail' => $records->item($i)->firstChild->nextSibling->textContent,
				'area' => $records->item($i)->lastChild->textContent);
				
			}

The problem is that the first field (firstChild) works, as does the firstChild->nextSibling, but of course the next lines are taking the same value as the previous line....

Can you help with the correct syntax to traverse XML inbetween firstChild and lastChild?

Thanks!

ps, really would like to complete this in PHP and not JavaScript....
mattward1979
Forum Newbie
Posts: 5
Joined: Fri Mar 04, 2011 10:11 am

Re: Populating an array through traversed XML

Post by mattward1979 »

as always I tend to stumble across the answers minutes after asking for help....

Code: Select all

$RecordsXML = array();
	for ($i=$first; $i<$last; $i++) 
			{
				$XML1 = $records->item($i)->getElementsByTagName("businessName"); 
				$XML1result  = $XML1->item(0)->nodeValue; 
		
				$XML2 = $records->item($i)->getElementsByTagName("phone"); 
				$XML2result  = $XML2->item(0)->nodeValue; 			
			
				$XML4 = $records->item($i)->getElementsByTagName("emPhone"); 
				$XML4result  = $XML4->item(0)->nodeValue; 
	
				$XML5 = $records->item($i)->getElementsByTagName("businessEmail"); 
				$XML5result  = $XML5->item(0)->nodeValue; 
	
				$XML6 = $records->item($i)->getElementsByTagName("area"); 
				$XML6result  = $XML6->item(0)->nodeValue; 
	

				
				$RecordsXML [] = array(
				'businessName' => $XML1result,
				'phone' =>  $XML2result,			
				'emPhone' => $XML4result,
				'businessEmail' => $XML5result,
				'area' => $XML6result);
				
			}

Im sure its far from the best solution, but it works...
Post Reply