Jump to content

XML Reader


jakre

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

Link to comment
https://forums.phpfreaks.com/topic/297786-xml-reader/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.