Parsing XML with nested namespaces in PHP

I received a XML with categories in it from Pohoda software from a client.
I struggled a bit with it, beacuse it contained nested namespaces what was new to me.
After figuring out it on my own I realized I could try google it and with luck I found ideas and code to reuse.

I came up with the following code to parse the XML:

$xml = file_get_contents(POHODA_CATEGORIES.xml);
$xml = simplexml_load_string($xml);
$ns = $xml->getNameSpaces(true);

foreach ($xml->xpath('//lst:categoryDetail') as $categories) {
    $ctg = $categories->children($ns['ctg']);
    foreach ($ctg->category as $category) {

        insert_page($category->id, $category->name);
        foreach ($c->subCategories->category as $subcategory) {
            insert_page($subcategory->id, $subcategory->name);

I also published a gist for it


Author: Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at Co-founded Blog at

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s