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.
-
<?
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();
-
-
if
(
is_writable
(
$xmlPath
)){
-
@file_put_contents
(
$xmlPath
,
$xmlData
);
-
}
Hope this helps. Thanks for reading.
From Mukesh Chapagain's Blog, post Magento: Read Write XML
分享到:
相关推荐
Magento-中文开发教程, 欢迎大家指出错误。 深入理解Magento-第一章 – Magento强大的配置系统 深入理解Magento-第二章 – Magento请求分发与控制器 深入理解Magento-第三章 – 布局,块和模板 深入理解...
magento-ce-2.4.0_sample_data-2020-07-26-02-51-57.tar.gz Include sample data
magento-1.8.0.0.tar.gz
Magento-Redismanager, 在 Magento,用于Redis的超级用户控制 Redis 管理Redis服务的实用程序特性系统> 配置>的自动或者手动配置高级> Redis管理在负载均衡环境中缓存服务不同的理想情况直接从magento面板管理员管理...
magento-ce-2.4.0-2020-07-24-11-15-38.tar.gz 不带sample data
magento-1.6.2.0 magento-1.6.2.0 magento-1.6.2.0
magento -redis 安装包
以详细的操作实例说明了magento的使用、配置、开发相关的实际应用。
因为国外网速太慢了,所以下载后分享给需要的人。magento-ce-2.4.2-p1-2021-04-22-10-49-09
最新的magento-1.7.0.2汉化包,汉化率达到98%上,绝对适合你
magento-演示数据1.6.1.0
在Magento 2站点内安装要在您的Magento 2项目中使用,可以使用: composer require --dev magento/magento-coding-standard 出于安全考虑,以这种方式安装时,无法自动添加phpcs的Magento标准。 您可以通过将以下...
magento-1.9.1.1-2015-04-30-12-49-08 源代码包
magento-1.6.1.0最新英文版
magento-1.7.0.7z,安装包
9. 运费设置--------------------------------------Magento——10.11 货运方式的设置 10. 支付接口设置 Magento——10.11 支付方式的设置 11. contact us 等 邮箱设置 Magento——10.12 Contact Us的邮箱设置
Ajax-Magento-ajax-add-to-cart.zip,[模块magento 1]magento ajax添加到购物车-ajoter vos produits au panier en ajax/感谢ajax将您的产品添加到购物车,ajax代表异步javascript和xml。它是多种web技术的集合,包括...
magento-stock-movements, Magento扩展,节省产品库存 节省你的Magento产品的库存 特性将库存移动保存在产品修改页面的新标签中安装 Magento 1.5. x, 1.6. x, 1.7. x, 1.8. x, 1.9.x使用 modgit 安装:$ cd/pat
Magento-CE-2.3.4_sample_data-2020-01-22-11-11-58.tar.gz源自官网
magento-enhancedemails-源码.rar