本文實例講述了php實現(xiàn)的數(shù)組轉(zhuǎn)xml。分享給大家供大家參考,具體如下:
0x00 需求
最近要做百度、360、神馬搜索的網(wǎng)站sitemap,三家的格式都是xml,然而具體的細(xì)節(jié)還有有差別的。
一開始用的是dom,沒有使用sax,寫了幾段便覺得太傻了,想到有沒有數(shù)組轉(zhuǎn)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,@表示屬性,%代表有屬性時這個元素本身的值,非常簡潔。
另外數(shù)組要把重復(fù)元素提到外面作為數(shù)組的key,重復(fù)元素的各種屬性是數(shù)組的值,并不需要像上面那樣指定 0、1、2索引,直接用就可以了。
0x02 改進(jìn)
可是發(fā)現(xiàn)有一個bug,根節(jié)點不能以CDATA開始。
另外還缺少一個功能,CDATA和屬性不能同時存在。
于是閱讀源碼,改進(jìn)了這兩項,提交給了作者,并被合并了。
我額外增加了一個符號 “!” ,當(dāng)CDATA 和屬性同時存在時,寫法為:
$input = [
"key" =>[
"@id" => 1,
"!" => 2
]
]
key id="1">![CDATA[2]]>/key>
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- linux下php安裝xml擴(kuò)展的詳細(xì)步驟
- php 使用expat方式解析xml文件操作示例
- PHP讀取XML文件的方法實例總結(jié)【DOMDocument及simplexml方法】
- PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
- 使用php操作xml教程