Read an xml file in php

suppose i have the following xml file named menu.xml

<menus>
<menu>
<name>Hello </name>
<link>index.php</link>
</menu>

</menus>

now i made the following php code snippet to get the info and store it in 2 different arrays.

$doc = new DOMDocument();
$menu_file = $params->get(‘menu_xml’);

$temp_file = $params->get(‘template’);
$doc->load($menu_file);
$menus_xml = $doc->getElementsByTagName(“menu”);
$count = 0;
foreach($menus_xml as $menu)
{
$menu_name = $menu->getElementsByTagName(“name”);

$link_name = $menu->getElementsByTagName(“link”);
$menus[$count] = $menu_name->item(0)->nodeValue;
$links[$count] = $link_name->item(0)->nodeValue;
$count++;
}

If you have a better option please inform me.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s