php实现的数组转xml案例分析

yipeiwu_com6年前PHP代码库

本文实例讲述了php实现的数组转xml。分享给大家供大家参考,具体如下:

0x00 需求

最近要做百度、360、神马搜索的网站sitemap,三家的格式都是xml,然而具体的细节还有有差别的。

一开始用的是dom,没有使用sax,写了几段便觉得太傻了,想到有没有数组转xml的库呢?

0x01 array2xml

搜索了一下,还真有地址为git,于是开始撸起袖子开始干。

示例如下:

THE CODE:

$xml = new ArrayToXML();
print $xml->buildXML($input);

INPUT:

$input = array('product' => array(
'@id' => 7,
'name' => 'some string',
'seo' => 'some-string',
'ean' => '',
'producer' => array(
'name' => null,
'photo' => '1.png'
),
'stock' => 123,
'trackstock' => 0,
'new' => 0,
'pricewithoutvat' => 1111,
'price' => 1366.53,
'discountpricenetto' => null,
'discountprice' => null,
'vatvalue' => 23,
'currencysymbol' => 'PLN',
'#description' => '',
'#longdescription' => '',
'#shortdescription' => '',
'category' => array(
'photo' => '1.png',
'name' => 'test3',
),
'staticattributes' => array(
'attributegroup' => array(
1 => array(
'@name' => 'attributes group',
'attribute' => array(
0 => array(
'name' => 'second',
'description' => 'desc2',
'file' => '',
),
1 =>
array(
'name' => 'third',
'description' => 'desc3',
'file' => '',
),
)
)
)
),
'attributes' => array(),
'photos' => array(
'photo' => array(
0 => array(
'@mainphoto' => '1',
'%' => '1.png',
),
1 => array(
'@mainphoto' => '0',
'%' => '2.png',
),
2 => array(
'@mainphoto' => '0',
'%' => '3.png',
)
)
)
));

OUTPUT (XML data):

<?xml version="1.0" encoding="UTF-8"?>
<data>
<product id="8">
<description><[CDATA[]]></description>
<longdescription><[CDATA[]]></longdescription>
<shortdescription><[CDATA[]]></shortdescription>
<name>some string</name>
<seo>some-string</seo>
<ean></ean>
<producer>
<name></name>
<photo>1.png</photo>
</producer>
<stock>123</stock>
<trackstock>0</trackstock>
<new>0</new>
<pricewithoutvat>1111</pricewithoutvat>
<price>1366.53</price>
<discountpricenetto></discountpricenetto>
<discountprice></discountprice>
<vatvalue>23</vatvalue>
<currencysymbol>PLN</currencysymbol>
<category>
<photo>1.png</photo>
<name>test3</name>
</category>
<staticattributes>
<attributegroup name="attributes group">
<attribute>
<name>second</name>
<description><p>desc2</p></description>
<file></file>
</attribute>
<attribute>
<name>third</name>
<description><p>desc3</p></description>
<file></file>
</attribute>
</attributegroup>
</staticattributes>
<photos>
<photo mainphoto="1">1.png</photo>
<photo mainphoto="0">2.png</photo>
<photo mainphoto="0">3.png</photo>
</photos>
</product>
</data>

可以看到,# 表示CDATA,@表示属性,%代表有属性时这个元素本身的值,非常简洁。
另外数组要把重复元素提到外面作为数组的key,重复元素的各种属性是数组的值,并不需要像上面那样指定 0、1、2索引,直接用就可以了。

0x02 改进

可是发现有一个bug,根节点不能以CDATA开始。

另外还缺少一个功能,CDATA和属性不能同时存在。

于是阅读源码,改进了这两项,提交给了作者,并被合并了。

我额外增加了一个符号 “!” ,当CDATA 和属性同时存在时,写法为:

$input = [
"key" =>[
"@id" => 1,
"!" => 2
]
]

<key id="1"><![CDATA[2]]></key>

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php实现多站点共用session实现单点登录的方法详解

php实现多站点共用session实现单点登录的方法详解

本文实例讲述了php实现多站点共用session实现单点登录的方法。分享给大家供大家参考,具体如下: 最近闲来无事,总结整理下单点登录的问题。 单点登录的基本原理为:客户端共享sesio...

Referer原理与图片防盗链实现方法详解

Referer原理与图片防盗链实现方法详解

本文实例讲述了Referer原理与图片防盗链实现方法。分享给大家供大家参考,具体如下: 1、图片防盗链 在一些大型网站中,比如百度贴吧,该站点的图片采用了防盗链的规则,以至于使用下面代码...

今天你说520了吗?不仅有php表白书还有java表白神器

今天你说520了吗?不仅有php表白书还有java表白神器

先来看看表白神器,点击这里下载,来表白吧! 不如就把这些感性的细枝末节放大吧,用代码写下520的“告白书” “自从遇见了你,就不停地想你。 编一个死循环, 让我们一直走,一直走。”...

解析二进制流接口应用实例 pack、unpack、ord 函数使用方法

在工作中,我也逐渐了解到pack,unpack,ord对于二进制字节处理的强大。 下面我逐一介绍它们。在我们工作中,用到它们的估计不多。 我在最近一个工作中,因为通讯需要用到二进制流,然...

扩展你的 PHP 之入门篇

扩展你的 PHP 之入门篇

扩展你的php 扩展你的php 扩展的3种方式 extension dll方式的扩展 小结   首先注意,以下所有的一切皆在 win 下进行,使用的工具的 VC++6.0。 扩展...