0x00 需求

最近要做百度、360、神马搜索的网站sitemap,三家的格式都是xml,然而具体的细节还有有差别的。

一开始用的是dom,没有使用sax,写了几段便觉得太傻了,想到有没有数组转xml的库呢?

0x01 array2xml

搜索了一下,还真有地址为git,于是开始撸起袖子开始干。

示例如下:

THE CODE:

  1. $xml = new ArrayToXML();
  2. print $xml->buildXML($input);

INPUT:


  1. $input = array('product' => array(
  2. '@id' => 7,
  3. 'name' => 'some string',
  4. 'seo' => 'some-string',
  5. 'ean' => '',
  6. 'producer' => array(
  7. 'name' => null,
  8. 'photo' => '1.png'
  9. ),
  10. 'stock' => 123,
  11. 'trackstock' => 0,
  12. 'new' => 0,
  13. 'pricewithoutvat' => 1111,
  14. 'price' => 1366.53,
  15. 'discountpricenetto' => null,
  16. 'discountprice' => null,
  17. 'vatvalue' => 23,
  18. 'currencysymbol' => 'PLN',
  19. '#description' => '',
  20. '#longdescription' => '',
  21. '#shortdescription' => '',
  22. 'category' => array(
  23. 'photo' => '1.png',
  24. 'name' => 'test3',
  25. ),
  26. 'staticattributes' => array(
  27. 'attributegroup' => array(
  28. 1 => array(
  29. '@name' => 'attributes group',
  30. 'attribute' => array(
  31. 0 => array(
  32. 'name' => 'second',
  33. 'description' => 'desc2',
  34. 'file' => '',
  35. ),
  36. 1 =>
  37. array(
  38. 'name' => 'third',
  39. 'description' => 'desc3',
  40. 'file' => '',
  41. ),
  42. )
  43. )
  44. )
  45. ),
  46. 'attributes' => array(),
  47. 'photos' => array(
  48. 'photo' => array(
  49. 0 => array(
  50. '@mainphoto' => '1',
  51. '%' => '1.png',
  52. ),
  53. 1 => array(
  54. '@mainphoto' => '0',
  55. '%' => '2.png',
  56. ),
  57. 2 => array(
  58. '@mainphoto' => '0',
  59. '%' => '3.png',
  60. )
  61. )
  62. )
  63. ));

OUTPUT (XML data):


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <data>
  3. <product id="8">
  4. <description><![CDATA[]]></description>
  5. <longdescription><![CDATA[]]></longdescription>
  6. <shortdescription><![CDATA[]]></shortdescription>
  7. <name>some string</name>
  8. <seo>some-string</seo>
  9. <ean></ean>
  10. <producer>
  11. <name></name>
  12. <photo>1.png</photo>
  13. </producer>
  14. <stock>123</stock>
  15. <trackstock>0</trackstock>
  16. <new>0</new>
  17. <pricewithoutvat>1111</pricewithoutvat>
  18. <price>1366.53</price>
  19. <discountpricenetto></discountpricenetto>
  20. <discountprice></discountprice>
  21. <vatvalue>23</vatvalue>
  22. <currencysymbol>PLN</currencysymbol>
  23. <category>
  24. <photo>1.png</photo>
  25. <name>test3</name>
  26. </category>
  27. <staticattributes>
  28. <attributegroup name="attributes group">
  29. <attribute>
  30. <name>second</name>
  31. <description><p>desc2</p></description>
  32. <file></file>
  33. </attribute>
  34. <attribute>
  35. <name>third</name>
  36. <description><p>desc3</p></description>
  37. <file></file>
  38. </attribute>
  39. </attributegroup>
  40. </staticattributes>
  41. <photos>
  42. <photo mainphoto="1">1.png</photo>
  43. <photo mainphoto="0">2.png</photo>
  44. <photo mainphoto="0">3.png</photo>
  45. </photos>
  46. </product>
  47. </data>

可以看到,# 表示CDATA,@表示属性,%代表有属性时这个元素本身的值,非常简洁。

另外数组要把重复元素提到外面作为数组的key,重复元素的各种属性是数组的值,并不需要像上面那样指定 0、1、2索引,直接用就可以了。

0x02 改进

可是发现有一个bug,根节点不能以CDATA开始。

另外还缺少一个功能,CDATA和属性不能同时存在。

于是阅读源码,改进了这两项,提交给了作者,并被合并了。

我额外增加了一个符号 “!” ,当CDATA 和属性同时存在时,写法为:

$input = [

"key" =>[

"@id" => 1,

"!" => 2

]

]

  1. <key id="1"><![CDATA[2]]></key>

php数组转xml的更多相关文章

  1. PHP中数组转换为XML格式

    最近公司要做一个API接口,输出格式要有JSON与XML格式, 在PHP中,输入JSON直接json_encode就可以,但输出XML没有提供函数,于是决定自己写一个. <?php /** * ...

  2. php数组转xml的递归实现

    原文:php数组转xml的递归实现 PHP中奖数组转为xml的需求是常见的,而且实现方法也有很多种,百度找了一下各种实现方法,但是基本是借组一些组件啥的.我就自己写了一个字符串拼组的方法,支持多维数组 ...

  3. PHP数组和XML相互转换的函数

    //数组转xml function ArrToXml($arr) { if(!is_array($arr) || count($arr) == 0) return ''; $xml = "& ...

  4. 数组转xml格式/xml格式转数组

    数组转xml格式 $arr=array( 'username'=>'huahua', 'password'=>'123456', 'number'=>'15889652911', ) ...

  5. php xml转数组,数组转xml,array转xml,xml转array

    //数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) ...

  6. PHP数组与xml互相转换

    1.数组转xml function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $va ...

  7. PowerShell 数组以及XML操作

    PowerShell基础 PowerShell数组操作 将字符串拆分成数据的操作 cls #原始字符串 $str = "abc,def,ghi,mon" #数据定义 #$StrAr ...

  8. 使用php将数组转为XML

    <?php class Array_to_Xml { private $version = '1.0'; private $encoding = 'UTF-8'; private $root = ...

  9. json 数组 对象 xml 之间转换(待补充)

    json 数组  xml 对象   之间转换(待补充) 1 把对象的类型或者数组转换成字符串类型(或者更确切的说是json类型的). 此处参考链接http://www.jb51.net/article ...

随机推荐

  1. 安卓listView实现下拉刷新上拉加载滑动仿QQ的删除功能

    大家对这些功能都是看的多了,然后对上拉刷新和下拉加载的原理都是非常清楚的,所以实现这功能其实也就是为了让大家能够从众多的同行们来进行比较学习而已,虽然即使是这样,但是面试的时候面试官还是会问你上拉和下 ...

  2. 【ASP.NET】@Model类型的使用详解

    有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NET Framework 4.0版本引入的System.Tuple类可以轻松满足这个需求. 假设Person和Produ ...

  3. Android的 EditText的inputType类型

    android开发过程中突然发现的warning EditText 报出 “This text field does not specify an inputType or a hint”   原因: ...

  4. MongoDB学习笔记(11) --- 聚合

    MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*) aggregate() 方法 MongoDB中聚 ...

  5. rhel 7 设置默认运行级别为图形

    查看默认级别 # systemctl get-default multi-user.target # cat /etc/inittab # inittab is no longer used when ...

  6. 从android aidl理解Proxy/stub模式

    在小7写的上一篇文章<android IPC通信机制梳理>里,我讲到了如果activity要想和一个跨进程的Service进行通信就需要通过Binder框架,获取到IBinder对象,并调 ...

  7. Android APK安装完成自动删除安装包

    需要实现此功能,一般实际开发是在自动版本更新上,当更新完开始自动安装完毕后,删除内存卡里的安装包.实现方式很简单,监听应用广播,获取内存卡下的文件,删除! 1.监听广播 package com.exa ...

  8. 有关java调用方法参数传递的分析

    这个问题好多文章都讲过了,在此本人补充一下,加深理解,有不足之处请指教. 相信做java开发同学们都知道,调用方法传递参数时,不论是基本类还是引用类型, java都是值传递,不存在引用传递(称引用传递 ...

  9. SpringBoot+SpringAOP+Java自定义注解+mybatis实现切库读写分离

    一.定义我们自己的切库注解类 自定义注解有几点需要注意: 1)@Target 是作用的目标,接口.方法.类.字段.包等等,具体看:ElementType 2)@Retention 是注解存在的范围,R ...

  10. Mac 下安装Ruby环境

    步骤1 - 安装 RVM RVM 是干什么的这里就不解释了,后面你将会慢慢搞明确. $ curl -L https://get.rvm.io | bash -s stable 期间可能会问你sudo管 ...