Loans - Remortgaging - Computer Help - Flights - Cheap Flights

PDA

View Full Version : Simple XML Parsing in PHP4?


Nick W
Oct 21st 2004, 1:14 pm
Hi all,

I have an xml file like this:

<container>
<item>
<val1>Some val</val1>
<val2></val2>
<val3>some other val</val3>
<val4>And yet another</val4>
</item>
</container>


There are hundreds of items.

What is the simplest functions to use to parse this so that i can get the values of <val1> and <val4>

Im happy to go read the manual but i hoped someone might point me in the right direction to get me started?

thanks...

Nick

digitalpoint
Oct 21st 2004, 1:33 pm
Assuming your XML document is in the variable $data...

$parser = xml_parser_create('UTF-8');
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);That will leave you with two arrays ($vals and $index) that will give you everything you need.

Nick W
Oct 21st 2004, 3:17 pm
>>xml_parse_into_struct

it all comes flooding back.. sheesh i have no memory, i actually did somthing similar once lol!

The only thing now is to try and get my head around the two array thing, i remember that being a real headache!

thanks shawn, very much obliged mate :)

Nick

TwisterMc
Oct 21st 2004, 3:37 pm
Oh I've wondered this for a while.

Ozz
Oct 25th 2004, 10:44 pm
If you have PEAR installed you can use the XML_SERIALIZER package.

You can get a XML file into an array and work with the array as you like. I can provide yo uwith an example and I think you can find tutorials in the web.

roy77
Jan 6th 2007, 6:56 pm
there are some tutorials i saw online, if you cnt find the answer here (and sry i cnt help u :( ) i would suggest you search yahoo or google