php: parsing xml

by prettyscripts on 2007-12-14 01:11:22

phpxml

in php5, parsing and processing xml can be easily done with SimpleXML extension.

to parse xml file:

PHP:

$obj simplexml_load_file($file);

to parse xml string:

PHP:

$obj simplexml_load_string($string);

example

XML:

<data>
    <tag>value 1<tag>
    <tag attribute="attrbute value">value 2<tag>
</data>

after loading xml into $obj, access tag values as class method:

PHP:

$obj->tag
$obj->tag[i//if more than 1 tag

access tag attribute as array:

PHP:

$obj->tag['attribute']
$obj->tag[i]['attribute'// if more than 1 tag

Tags: php, xml

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)