博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
magento -- 用Magento的方式读写XML
阅读量:4199 次
发布时间:2019-05-26

本文共 2654 字,大约阅读时间需要 8 分钟。

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. // 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.
From Mukesh Chapagain's Blog, post Magento: Read Write XML

 

转载地址:http://vwcli.baihongyu.com/

你可能感兴趣的文章
高性能服务器设计
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>
HTML5学习之——HTML 5 服务器发送事件
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>
解决Rhythmbox乱码
查看>>
豆瓣爱问共享资料插件发布啦
查看>>
kermit的安装和配置
查看>>
linux中cat命令使用详解
查看>>
java中的异常机制
查看>>
商务智能-基本方法-数据钻取
查看>>
openstack-instance-high-availability-Evacuate
查看>>
evacuate-instance-automatically
查看>>
pycharm常用设置(keymap设置及eclipse常用快捷键总结)
查看>>