您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)利用php怎么將數(shù)組轉(zhuǎn)為xml,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
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開始。
另外還缺少一個(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>
關(guān)于利用php怎么將數(shù)組轉(zhuǎn)為xml就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。