`
coolsooner
  • 浏览: 1363691 次
文章分类
社区版块
存档分类
最新评论

magento -- 用Magento的方式读写XML

 
阅读更多

magento -- 用Magento的方式读写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 >


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>" ;


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. //checkiftheXMLfileiswritableandthensavedata
  6. if ( is_writable ( $xmlPath )){
  7. @file_put_contents ( $xmlPath , $xmlData );
  8. }


Hope this helps. Thanks for reading.

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

分享到:
评论

相关推荐

    magento-custom-address:一个简单的模块,可将“邻居”和“互补”字段添加到Magento商店中的客户地址

    通过安装并启用这个模块,Magento用户就能在他们的商店中看到并使用这些新的地址字段了。 总的来说,这个模块展示了Magento如何通过扩展机制来满足特定业务需求,同时也为我们提供了一个学习和理解Magento模块开发...

    magento入门文档

    这里设置了`helloworld`路由,使用标准路由方式,并指定了模块名称和前端名称。 **2.6 Magento名词解释** - **模型**(Model):负责处理数据操作,包括读取、写入等。 - **视图**(View):负责用户界面的显示,...

    Magento 添加后台管理

    Magento提供了角色和权限管理,开发者可以通过`app/code/{Vendor}/{Module}/etc/adminhtml/acl.xml`文件来设定新模块的访问权限,控制不同角色的管理员能否访问该功能。 9. **事件观察者** 如果需要在特定操作...

    magento安装过程中和安装后的问题解决

    查看Magento的配置文件(如`app/etc/local.xml`)中关于基础URL的设置是否包含非法字符。对于上述错误,可以考虑使用以下代码替换: ```php if (false !== strpos($secureBaseUrl, '{{base_url}}')) { $...

    Magento本地安装

    Magento是一款开源的电子商务平台,由Adobe公司开发,用于构建功能丰富的在线商店。它以其灵活性、可扩展性和强大的功能而闻名。在本篇文章中,我们将详细探讨如何在本地环境进行Magento的安装过程,这对于开发者...

    Solvingmagento_OneStepCheckout:Solvingmagento.com OneStep 结账教程

    1. 兼容性问题:确保扩展与你当前使用的Magento版本兼容。如果不兼容,可能需要寻找更新或修改源码。 2. 功能冲突:与其他结账插件并用时可能出现功能冲突,关闭或禁用其他插件,查看问题是否得到解决。 3. 错误日志...

    PHP经典实例 第3版_PDF电子书下载 高清 带索引书签目录

    6. PHP与XML:解析XML文档,使用SimpleXML和DOM扩展进行数据操作。 7. 文件系统操作:读写文件、创建目录、处理文件上传和下载。 8. PHP安全:预防SQL注入、XSS攻击,了解如何使用过滤函数和验证码技术。 9. PHP...

Global site tag (gtag.js) - Google Analytics