主頁(yè) > 知識(shí)庫(kù) > php實(shí)現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析

php實(shí)現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析

熱門標(biāo)簽:團(tuán)購(gòu)網(wǎng)站 Linux服務(wù)器 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 阿里云 電子圍欄 服務(wù)器配置 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù)

本文實(shí)例講述了php實(shí)現(xiàn)的數(shù)組轉(zhuǎn)xml。分享給大家供大家參考,具體如下:

0x00 需求

最近要做百度、360、神馬搜索的網(wǎng)站sitemap,三家的格式都是xml,然而具體的細(xì)節(jié)還有有差別的。

一開(kāi)始用的是dom,沒(méi)有使用sax,寫了幾段便覺(jué)得太傻了,想到有沒(méi)有數(shù)組轉(zhuǎn)xml的庫(kù)呢?

0x01 array2xml

搜索了一下,還真有地址為git,于是開(kāi)始擼起袖子開(kāi)始干。

示例如下:

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í)這個(gè)元素本身的值,非常簡(jiǎn)潔。
另外數(shù)組要把重復(fù)元素提到外面作為數(shù)組的key,重復(fù)元素的各種屬性是數(shù)組的值,并不需要像上面那樣指定 0、1、2索引,直接用就可以了。

0x02 改進(jìn)

可是發(fā)現(xiàn)有一個(gè)bug,根節(jié)點(diǎn)不能以CDATA開(kāi)始。

另外還缺少一個(gè)功能,CDATA和屬性不能同時(shí)存在。

于是閱讀源碼,改進(jìn)了這兩項(xiàng),提交給了作者,并被合并了。

我額外增加了一個(gè)符號(hào) “!” ,當(dāng)CDATA 和屬性同時(shí)存在時(shí),寫法為:

$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針對(duì)XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • linux下php安裝xml擴(kuò)展的詳細(xì)步驟
  • php 使用expat方式解析xml文件操作示例
  • PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
  • PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
  • 使用php操作xml教程

標(biāo)簽:廣元 大理 江蘇 衡水 萍鄉(xiāng) 衢州 蚌埠 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php實(shí)現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266