processing xml with php

Categories: php, xml

Tags: xml

before php v5, there are a set of xml functions to parse xml but i can never remember how to use them. it was complicated.

since v5, parsing and processing xml has never been easier with SimpleXML extension.

to parse xml file:

PHP:

$obj simplexml_load_file($file);

to parse xml string:

PHP:

$obj simplexml_load_string($string);

xml 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

Affliates

Spreadfirefox Affiliate Button