濮阳杆衣贸易有限公司

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

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

熱門標(biāo)簽:智能電話機(jī)器人好公司門薩維 沛縣400電話辦理 辦理重慶400電話 聊城電話外呼系統(tǒng)公司 青白江地圖標(biāo)注 銅川電話機(jī)器人價格 AI電話機(jī)器人OEM貼牌 江蘇電商外呼系統(tǒng)運營商 德陽中江如何申請400開頭電話

本文實例講述了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教程

標(biāo)簽:南寧 迪慶 山南 赤峰 烏魯木齊 三亞 濟(jì)寧 鷹潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php實現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析》,本文關(guān)鍵詞  php,實現(xiàn),的,數(shù)組,轉(zhuǎn),xml,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php實現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析》相關(guān)的同類信息!
  • 本頁收集關(guān)于php實現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    苗栗县| 陇南市| 明星| 五原县| 河间市| 东丰县| 白玉县| 万安县| 兴隆县| 法库县| 临夏市| 肥东县| 灵台县| 信宜市| 东乡族自治县| 墨竹工卡县| 东源县| 海城市| 北宁市| 诸城市| 浦县| 遵义市| 沈丘县| 封丘县| 屯留县| 讷河市| 鄂伦春自治旗| 北流市| 石棉县| 灵宝市| 泰州市| 临潭县| 揭阳市| 彭泽县| 木里| 西华县| 桃园县| 天台县| 舞钢市| 靖西县| 从江县|