利用php怎么将数组转为xml

  介绍

这篇文章将为大家详细讲解有关利用php怎么将数组转为xml,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

代码:

xml 美元;=,new  ArrayToXML ();   print  $ xml→buildXML(输入)美元;输入:

input 美元;=,阵列(& # 39;产品# 39;,=祝辞,阵列(   & # 39;@ id # 39;,=祝辞,7日   & # 39;名字# 39;,=祝辞,& # 39;some 字符串# 39;   & # 39;seo # 39;,=祝辞,& # 39;some-string& # 39;   & # 39;ean # 39;,=祝辞,& # 39;& # 39;   & # 39;制片人# 39;,=祝辞,阵列(   & # 39;名字# 39;,=祝辞,空,   & # 39;照片# 39;,=祝辞,& # 39;1. png # 39;   ),   & # 39;股票# 39;,=祝辞,123年,   & # 39;trackstock& # 39;,=祝辞,0,   & # 39;新# 39;,=祝辞,0,   & # 39;pricewithoutvat& # 39;,=祝辞,1111年,   & # 39;价格# 39;,=祝辞,1366.53,   & # 39;discountpricenetto& # 39;,=祝辞,空,   & # 39;discountprice& # 39;,=祝辞,空,   & # 39;vatvalue& # 39;,=祝辞,23岁,   & # 39;currencysymbol& # 39;,=祝辞,& # 39;pln # 39;   & # 39;#描述# 39;,=祝辞,& # 39;& # 39;   & # 39;# longdescription& # 39;,=祝辞,& # 39;& # 39;   & # 39;# shortdescription& # 39;,=祝辞,& # 39;& # 39;   & # 39;类别# 39;,=祝辞,阵列(   & # 39;照片# 39;,=祝辞,& # 39;1. png # 39;   & # 39;名字# 39;,=祝辞,& # 39;test3 # 39;   ),   & # 39;staticattributes& # 39;,=祝辞,阵列(   & # 39;attributegroup& # 39;,=祝辞,阵列(   1,=祝辞,阵列(   & # 39;@ name # 39;,=祝辞,& # 39;attributes 组# 39;   & # 39;属性# 39;,=祝辞,阵列(   0,=祝辞,阵列(   & # 39;名字# 39;,=祝辞,& # 39;第二# 39;   & # 39;描述# 39;,=祝辞,& # 39;desc2& # 39;   & # 39;文件# 39;,=祝辞,& # 39;& # 39;   ),   1,=比;   阵列(   & # 39;名字# 39;,=祝辞,& # 39;第三# 39;   & # 39;描述# 39;,=祝辞,& # 39;desc3& # 39;   & # 39;文件# 39;,=祝辞,& # 39;& # 39;   ),   )   )   )   ),   & # 39;属性# 39;,=祝辞,数组(),   & # 39;照片# 39;,=祝辞,阵列(   & # 39;照片# 39;,=祝辞,阵列(   0,=祝辞,阵列(   & # 39;@mainphoto& # 39;,=祝辞,& # 39;1 & # 39;   & # 39;% & # 39;,=祝辞,& # 39;1. png # 39;   ),   1,=祝辞,阵列(   & # 39;@mainphoto& # 39;,=祝辞,& # 39;0 & # 39;   & # 39;% & # 39;,=祝辞,& # 39;2. png # 39;   ),   2,=祝辞,阵列(   & # 39;@mainphoto& # 39;,=祝辞,& # 39;0 & # 39;   & # 39;% & # 39;,=祝辞,& # 39;3. png # 39;   )   )   )   ));

<强>输出(XML数据):

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   & lt; data>   & lt; product  id=?“在   & lt; description> & lt; [CDATA[]]祝辞& lt;/description>   & lt; longdescription> & lt; [CDATA[]]祝辞& lt;/longdescription>   & lt; shortdescription> & lt; [CDATA[]]祝辞& lt;/shortdescription>   & lt; name> some  string</name>   & lt; seo> some-string   & lt; ean> & lt;/ean>   & lt; producer>   & lt; name> & lt;/name>   & lt; photo> 1. png   & lt;/producer>   & lt; stock> 123 & lt;/stock>   & lt; trackstock> 0 & lt;/trackstock>   & lt; new> 0 & lt;/new>   & lt; pricewithoutvat> 1111 & lt;/pricewithoutvat>   & lt; price> 1366.53 & lt;/price>   & lt; discountpricenetto> & lt;/discountpricenetto>   & lt; discountprice> & lt;/discountprice>   & lt; vatvalue> 23 & lt;/vatvalue>   & lt; currencysymbol> PLN   & lt; category>   & lt; photo> 1. png   & lt; name> test3   & lt;/category>   & lt; staticattributes>   & lt; attributegroup  name=癮ttributes  group"比;   & lt; attribute>   & lt; name> second   & lt; description> & lt; p> desc2

& lt;/description>   & lt; file> & lt;/file>   & lt;/attribute>   & lt; attribute>   & lt; name> third   & lt; description> & lt; p> desc3

& lt;/description>   & lt; file> & lt;/file>   & lt;/attribute>   & lt;/attributegroup>   & lt;/staticattributes>   & lt; photos>   & lt; photo  mainphoto=?“在1. png   & lt; photo  mainphoto=?”在2. png   & lt; photo  mainphoto=?”在3. png   & lt;/photos>   & lt;/product>   & lt;/data>

可以看的到,#表示CDATA, @表示属性,%代表有属性时这个元素本身的值,非常简洁。
另外数组要把重复元素提到外面作为数组的关键,重复元素的各种属性是数组的值,并不需要像上面那样指定0,1,2索引,直接用就可以了。

利用php怎么将数组转为xml