Varien_Simplexml_Config

本文介绍了如何使用Varien_Simplexml_Element类在Magento代码中读取XML节点,并通过示例代码展示如何加载XML文件、读取节点数据以及在XML文件中添加新节点。

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.

  1. <? xml   version = "1.0" ?>   
  2. < config >   
  3. < modules >   
  4. < MyNamespace_MyModule >   
  5. < version > 0.1.0 </ version >   
  6. </ MyNamespace_MyModule >   
  7. </ modules >   
  8. < frontend >   
  9. < routers >   
  10. < mymodule >   
  11. < use > standard </ use >   
  12. < args >   
  13. < module > MyNamespace_MyModule </ module >   
  14. < frontName > mymodule </ frontName >   
  15. </ args >   
  16. </ mymodule >   
  17. </ routers >   
  18. < layout >   
  19. < updates >   
  20. < mymodule >   
  21. < file > mymodule.xml </ file >   
  22. </ mymodule >   
  23. </ updates >   
  24. </ layout >   
  25. </ frontend >   
  26. </ config >   
  1. <?xml version="1.0"?>  
  2. <config>  
  3. <modules>  
  4. <MyNamespace_MyModule>  
  5. <version>0.1.0</version>  
  6. </MyNamespace_MyModule>  
  7. </modules>  
  8. <frontend>  
  9. <routers>  
  10. <mymodule>  
  11. <use>standard</use>  
  12. <args>  
  13. <module>MyNamespace_MyModule</module>  
  14. <frontName>mymodule</frontName>  
  15. </args>  
  16. </mymodule>  
  17. </routers>  
  18. <layout>  
  19. <updates>  
  20. <mymodule>  
  21. <file>mymodule.xml</file>  
  22. </mymodule>  
  23. </updates>  
  24. </layout>  
  25. </frontend>  
  26. </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.
  1. $xmlPath  Mage::getBaseDir().DS. 'test.xml' ;  
  2. $xmlObj   new  Varien_Simplexml_Config( $xmlPath );  
  3. $xmlData   $xmlObj ->getNode();  
  4. echo   "<pre>" ; print_r( $xmlData );  echo   "</pre>" ;  
  1. $xmlPath Mage::getBaseDir().DS.'test.xml';  
  2. $xmlObj new Varien_Simplexml_Config($xmlPath);  
  3. $xmlData $xmlObj->getNode();  
  4. 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’.
  1. $xmlPath  Mage::getBaseDir().DS. 'test.xml' ;  
  2. $xmlObj   new  Varien_Simplexml_Config( $xmlPath );  
  3. $xmlObj ->setNode( 'modules/mukesh' , 'chapagain' );  
  4. $xmlData   $xmlObj ->getNode()->asNiceXml();  
  5. // check if the XML file is writable and then save data   
  6. if ( is_writable ( $xmlPath ))  
  7. @file_put_contents ( $xmlPath ,  $xmlData );  
  8.  
  1. $xmlPath Mage::getBaseDir().DS.'test.xml';  
  2. $xmlObj new Varien_Simplexml_Config($xmlPath);  
  3. $xmlObj->setNode('modules/mukesh','chapagain');  
  4. $xmlData $xmlObj->getNode()->asNiceXml();  
  5. // check if the XML file is writable and then save data  
  6. if(is_writable($xmlPath))  
  7. @file_put_contents($xmlPath, $xmlData);  
  8.  


Hope this helps. Thanks for reading.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值