php實現(xiàn)的數(shù)組轉xml案例分析
本文實例講述了php實現(xiàn)的數(shù)組轉xml。分享給大家供大家參考,具體如下:
0x00 需求
最近要做百度、360、神馬搜索的網(wǎng)站sitemap,三家的格式都是xml,然而具體的細節(jié)還有有差別的。
一開始用的是dom,沒有使用sax,寫了幾段便覺得太傻了,想到有沒有數(shù)組轉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ù)組要把重復元素提到外面作為數(shù)組的key,重復元素的各種屬性是數(shù)組的值,并不需要像上面那樣指定 0、1、2索引,直接用就可以了。
0x02 改進
可是發(fā)現(xiàn)有一個bug,根節(jié)點不能以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相關內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
php使用simplexml_load_file加載XML文件并顯示XML的方法
這篇文章主要介紹了php使用simplexml_load_file加載XML文件并顯示XML的方法,實例分析了simplexml_load_file操作XML文件的技巧,非常具有實用價值,需要的朋友可以參考下2015-03-03解析:通過php socket并借助telnet實現(xiàn)簡單的聊天程序
本篇文章是對通過php socket并借助telnet實現(xiàn)簡單聊天程序的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06