Forum Moderators: coopster

Message Too Old, No Replies

How obtain the three telephones in this xml?

with php. Suggestions of parses please

         

Corretja

4:55 pm on Jul 9, 2006 (gmt 0)

10+ Year Member



Hi,

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]

coopster

5:37 pm on Jul 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Corretja.

Are you running PHP5? If so, you could use SimpleXML [us2.php.net] which makes this very very easy.

Corretja

5:45 pm on Jul 9, 2006 (gmt 0)

10+ Year Member



Thanks for the welcome Coopster.

No, im using php4 yet .

coopster

6:02 pm on Jul 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hmmm, too bad, it sure makes XML work a ton easier. Otherwise, I'm not familiar with that particular class -- you may have to check the project documentation, see if they have a forum, or contact them directly.

Corretja

6:14 pm on Jul 9, 2006 (gmt 0)

10+ Year Member



Plz can you put the php code using SimpleXML to gather the 3 phones? My english is bad .

I could upgrate to php5

Duskrider

5:40 am on Jul 10, 2006 (gmt 0)

10+ Year Member



Hey Corretja,

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].

Corretja

4:56 pm on Jul 11, 2006 (gmt 0)

10+ Year Member



Thanks Duskrider. I will test it and reply with my tests.

[edited by: Corretja at 4:56 pm (utc) on July 11, 2006]