Home » Magento, XML

Magento: Read Write XML

17 February 2010 2,699 views Popularity: 6% Share/Bookmark

I will be using Varien_Simplexml_Element class to read write xml nodes. The path to this class file is lib/Varien/Simplexml/Element.php

Here is a sample XML file which I am going to read through Magento code. I will also be adding an XML node to the following XML data.

<?xml version="1.0"?>
<config>
    <modules>
        <MyNamespace_MyModule>
            <version>0.1.0</version>
        </MyNamespace_MyModule>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>MyNamespace_MyModule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
        <layout>
            <updates>
                <mymodule>
                    <file>mymodule.xml</file>
                </mymodule>
            </updates>
        </layout>
    </frontend>
</config>

Here is the Magento/PHP code to read the XML data. I have kept the XML file in the root directory of Magento installation. The XML file is named test.xml. At first, the XML file is loaded and then it’s node are read with getNode function. Then, I have printed the result.

$xmlPath = Mage::getBaseDir().DS.'test.xml';
$xmlObj = new Varien_Simplexml_Config($xmlPath);
$xmlData = $xmlObj->getNode();
echo "<pre>"; print_r($xmlData); echo "</pre>";

You can add node with the setNode function. Here, I have set a node inside the node ‘modules’. The name of my new node is ‘mukesh’ and it’s value is ‘chapagain’.

$xmlPath = Mage::getBaseDir().DS.'test.xml';
$xmlObj = new Varien_Simplexml_Config($xmlPath);

$xmlObj->setNode('modules/mukesh','chapagain');

$xmlData = $xmlObj->getNode()->asNiceXml();

// check if the XML file is writable and then save data
if(is_writable($xmlPath)) {
	@file_put_contents($xmlPath, $xmlData);
}

Hope this helps. Thanks for reading.

From Mukesh Chapagain's Blog, post Magento: Read Write XML

Related posts:

  1. Magento: Read config XML nodes
  2. PHP : Read Write Xml with DOMDocument
  3. PHP : Read Write Xml with SimpleXML
  4. PHP: Read Write CSV
  5. Magento: How to get list of all modules programmatically?
  6. Magento: Helper Data not found error
  7. Magento: Create, read, delete cookie
  8. Magento: Upgrading mysql setup of a module
  9. Magento: Set title, keywords and description in your module
  10. Magento: Access denied in admin of custom module
  • Pingback: Timon takes on Nichols at 9 a.m. Friday – Metro WNY Blogs, Buffalo … | Buffalo Bills NFL Announcer

  • Pingback: Magento: Read Write XML | Mukesh Chapagain's Blog | Magento News

  • http://www.best-website-designer.com Best Website Designer

    I have been looking help for my Magento related queries, this was one of my query, as I ma new to Magento. I have been learning it around in internet. I think I will visit your website daily to get good knowledge on Magento.
    magento themes

  • Mariawi9232

    Do you have an idea how can you add attributes into those created XML elements?

  • jaikanth123

    This query was really helpfull to me because i was not knowing to configure XML configuration.

  • http://www.mgtdesign.co.uk/web-design-northampton/ Kelton

    Nice tutor.Your way of presentation is too good.I like the information you have managed & compile.Its really a unique & real information.I would like to read your more updates in future too.Keep in touch with us.

  • http://www.totallyseo.co.uk/ Yarout

    Nice and smart written article.I really liked it a lot.Thanks a lot for sharing such article with us.Keep sharing.