Avatar of yarek
yarek
Flag for France asked on

XML parser in php 4.3

I am trying to parse some XML in PHP 4.3 (not PHP5)
I have found some much info about XML in PHP that I am just lost

Globally what I need is:
giving a string
$str="
<root>
<users>
  <user>Mike</user>
  <user>Richard</user>
  <user>Ana</user>
</users>
</root>";
// here I need a function that echo :
user 1:Mike
user 1:Richard
user 1:Ana

I need a standard XML funtions or libraries (no string parsing) in PHP 4.3 or above

PHPProgramming Languages-Other

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
yarek

ASKER
I believe it can be done in PHP 4.3 as well !
I really do not need links: I have thousands of them: just a little snippet of code to do this simple XML parsing
Ray Paseur

Of course it can be done in PHP 4.3 (it could be done in FORTRAN) - but nobody wants to do OO development in PHP 4 when PHP 5 is available.  PHP 4 requires you to reinvent things that have already been invented in PHP 5.

But that said, if you follow this tutorial, you can cut the snippets of code you find appropriate to your requirements.
http://www.developertutorials.com/tutorials/php/parsing-xml-using-php4-050816/page1.html

If all you want to do is parse what you posted above... you can do a lot with explode();
http://us.php.net/manual/en/function.explode.php
<?php
function ez_xml_extract($string, $tag) {
// FIND THE OPENING TAGS IF ANY EXIST
	$tag_array = explode('<' . $tag . '>', $string);
	if (empty($tag_array)) {
return FALSE;
}
// DISCARD THE UNUSED STUFF (PROBABLY <?XML TAG)
	unset($tag_array[0]);
// ITERATE OVER THE RESULTING CONTENTS, COLLECTING THE OUTPUT
	foreach ($tag_array as $tag_data) {
		$my_tag	= explode('</' . $tag . '>', $tag_data);
		$output[] = $my_tag[0];
	}
return $output;
} // ! EZ_XML_EXTRACT
 
$str="
<root>
<users>
  <user>Mike</user>
  <user>Richard</user>
  <user>Ana</user>
</users>
</root>";
var_dump(ez_xml_extract($str, 'user'));
 
$non_str="
<root>
<users>
  <nouser>Mike</nouser>
  <nouser>Richard</nouser>
  <nouser>Ana</nouser>
</users>
</root>";
var_dump(ez_xml_extract($non_str, 'user'));
 
?>

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy