magento -- 用Magento的方式读写XML

2024-03-25 14:58
文章标签 xml 方式 读写 magento

本文主要是介绍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.

view plain copy to clipboard print ?
  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.
view plain copy to clipboard print ?
  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’.
view plain copy to clipboard print ?
  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

 

这篇关于magento -- 用Magento的方式读写XML的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/845355

相关文章

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

Linux中的计划任务(crontab)使用方式

《Linux中的计划任务(crontab)使用方式》:本文主要介绍Linux中的计划任务(crontab)使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言1、linux的起源与发展2、什么是计划任务(crontab)二、crontab基础1、cro

Win11安装PostgreSQL数据库的两种方式详细步骤

《Win11安装PostgreSQL数据库的两种方式详细步骤》PostgreSQL是备受业界青睐的关系型数据库,尤其是在地理空间和移动领域,:本文主要介绍Win11安装PostgreSQL数据库的... 目录一、exe文件安装 (推荐)下载安装包1. 选择操作系统2. 跳转到EDB(PostgreSQL 的