Forum Moderators: coopster
Im learning the xml. I can parse (analize/read) all the xml without problems less when the xml has childrens with the same path and name. For example :
<Result>
<person:name>Albert</person:name>
<person:detailinfo>
<person:address>central square</person:address>
<person:city>NY</person:city>
<person:phone>915658767</person:phone>
<person:phone>902345644</person:phone>
<person:phone>677234345</person:phone>
</person:detailinfo>
</Result>
I receive results in xml (as in the example) in a php variable ($xml_result) and i need show in screen the 3 phones of Albert. How can i do that with php?
Now im using the minixml parser ( [sourceforge.net...] ) and works ok i can gather the <person:phone> but only the first match , i cant gather the rest. Can count the number of childers and if i know how is the xml result then with the position and one for get the others phone, but is a bad method because sometimes the number of results (childrens) of the xml resturned it varies (also the position)
How can i gather exactly the 3 phones? Please if somebody can put in php an example to me to do it... would be very appreciate. not necessarily with minixml parse, with any other parser of php or the default functions of php for xml (im not sure if it possible with them)
Many thanks and sorry for my bad english
[edited by: coopster at 5:56 pm (utc) on July 9, 2006]
[edit reason] updated url to open source project page [/edit]
Are you running PHP5? If so, you could use SimpleXML [us2.php.net] which makes this very very easy.
I'm fairly inexperienced with regular expressions and haven't used them that much, but it seems to me that this would be the best way to do what you want. (Someone please correct me if my regex is not correct. *grin*)
<?php
preg_match_all("/<person:phone>([0-9]+)<\/person:phone>/",$xml_result,$match);
foreach ($match[1] as $phone) {
//Do whatever you want to $phone
echo $phone;
}
?>
That should give you all your phone numbers in the array $match[1].