Jump to content

XML Reader


jakre
Go to solution Solved by Barand,

Recommended Posts

Hello,

 

I have a problem with my XML reader which I have just done. The code of this reader is the same as in this video because I am beginner and I made it according to it. This is how the PHP file looks like:

<?php

$xml = new XMLReader();
$xml->open('members.xml');

$members = array();


while($xml->read()) {

	if($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == 'member') {
		$members = array();
		$members['id'] = $xml->getAttribute('id');
	}
	
	if($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == 'firstName') {
		$xml->read();
		$members['firstName'] = $xml->value;
		
	}
	
	if($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == 'lastName') {
		$xml->read();
		$members['lastName'] = $xml->value;
		
		$members[] = $member;
	
	}
	
}

echo displayMembers($members);

function displayMembers($members) {
	
	$r = '';

	// build it
	if(count($members) > 0) {
		$r .= '<table>';
		foreach($members as $member) {
			
			$r .= '<tr>';
			$r .= '<td style="background-color:#eee; padding:3px">' . $member['id'] . '</td>';
			$r .= '<td style="background-color:#eee; padding:3px">' . $member['teplotaden'] . '</td>';
			$r .= '<td style="background-color:#eee; padding:3px">' . $member['teplotanoc'] . '</td>';
			$r .= '</tr>';
			
		}
		$r .= '</table>';	
	}
	
	return $r;
	
}

?>

This reader displays the table with first and last names from the XML file. The problem is that it displays all the names in a table and I want to display only the first name (read only the 3 - 7 line), I mean the content from the first attribute in a XML file:

<members>
   <member id="1">
      <firstName>Jim</firstName>
      <lastName>Smith</lastName>
      ...
   </member>
   <member id="2">
      <firstName>Hank</firstName>
      <lastName>Rogers</lastName>
      ...
   </member>
   ...
</members>

My actual table looks like this:

1 Jim Smith
2 Hank Rogers
...

Thanks for help in advance,

jakre

Edited by jakre
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.