Home » Magento, XML

Magento: Read Write XML

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

email

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.

Related posts:

  1. PHP : Read Write Xml with SimpleXML
  2. PHP : Read Write Xml with DOMDocument
  3. Magento: Read config XML nodes
  4. PHP: Read Write CSV
  5. Magento: Helper Data not found error
  6. Magento: Create, read, delete cookie
  7. Magento: How to get list of all modules programmatically?
  8. Magento: Set title, keywords and description in your module
  9. Magento: Upgrading mysql setup of a module
  10. jQuery: Grey out background and preview image as popup
  • http://buffalo-bills.nflannouncer.com/timon-takes-on-nichols-at-9-a-m-friday-metro-wny-blogs-buffalo/ Timon takes on Nichols at 9 a.m. Friday – Metro WNY Blogs, Buffalo … | Buffalo Bills NFL Announcer

    [...] Magento: Read Write XML | Mukesh Chapagain's Blog [...]

  • http://www.magentonews.co.uk/magnento/magento-read-write-xml-mukesh-chapagains-blog/ Magento: Read Write XML | Mukesh Chapagain's Blog | Magento News

    [...] Magento: Read Write XML | Mukesh Chapagain's Blog [...]

  • 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.