From 1b3f366a59f71d20e508866128682acab23ea8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Sat, 20 Apr 2013 12:27:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4xml=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E6=98=AF=E7=94=A8PHP=E5=86=85?= =?UTF-8?q?=E7=BD=AE=E7=9A=84SimpleXMLElement=E5=AF=B9=E8=B1=A1=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Transform/Driver/Xml.php | 71 ++++++++++++++------------ 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/Library/Think/Transform/Driver/Xml.php b/Library/Think/Transform/Driver/Xml.php index bc6543de..0e0d20f7 100644 --- a/Library/Think/Transform/Driver/Xml.php +++ b/Library/Think/Transform/Driver/Xml.php @@ -21,7 +21,6 @@ class Xml{ 'root_attr' => [], //根节点属性 'item_name' => 'item', //数字节点转换的名称 'item_key' => 'id', //数字节点转换的属性名 - 'encoding' => 'utf-8', //字符集编码 ]; /** @@ -34,29 +33,31 @@ class Xml{ //初始化配置 $config = array_merge($this->config, $config); - if(is_array($config['root_attr'] && !empty($config['root_attr']))){ - $attr = []; - foreach ($config['root_attr'] as $key => $value) { - $attr[] = "{$key}=\"{$value}\""; - } - $attr = implode(' ', $attr); - } - $attr = empty($attr) ? '' : " {$attr}"; - $xml = ""; - $xml .= "<{$config['root_name']}{$attr}>"; - $xml .= self::data2xml($data, $config['item_name'], $config['item_key']); - $xml .= ""; - return $xml; + //创建XML对象 + $xml = new \SimpleXMLElement("<{$config['root_name']}>"); + self::data2xml($xml, $data, $config['item_name'], $config['item_key']); + return $xml->asXML(); } - public function decode($data, $assoc = true, array $config = []){ + /** + * 解码XML数据 + * @param string $str XML字符串 + * @param boolean $assoc 是否转换为数组 + * @param array $config 数据配置项 + * @return string 解码后的XML数据 + */ + public function decode($str, $assoc = true, array $config = []){ //初始化配置 $config = array_merge($this->config, $config); //创建XML对象 - $xml = new SimpleXMLElement($data); - self::xml2data($xml, $data, $config['item_name'], $config['item_key']); - return $data; + $xml = new \SimpleXMLElement($str); + if($assoc){ + self::xml2data($xml, $data, $config['item_name'], $config['item_key']); + return $data; + } + + return $xml; } /** @@ -68,18 +69,25 @@ class Xml{ * @param string $id 数字索引key转换为的属性名 * @return string */ - static public function data2xml($data, $item = 'item', $id = 'id') { - $xml = $attr = ''; - foreach ($data as $key => $val) { + static public function data2xml(SimpleXMLElement $xml, $data, $item = 'item', $id = 'id') { + foreach ($data as $key => $value) { + //指定默认的数字key if(is_numeric($key)){ - $id && $attr = " {$id}=\"{$key}\""; + $id && $val = $key; $key = $item; } - $xml .= "<{$key}{$attr}>"; - $xml .= (is_array($val) || is_object($val)) ? self::data2xml($val, $item, $id) : $val; - $xml .= ""; + + //添加子元素 + if(is_array($value) || is_object($value)){ + $child = $xml->addChild($key); + self::data2xml($child, $value, $item, $id); + } else { + $child = $xml->addChild($key, $value); + } + + //记录原来的key + isset($val) && $child->addAttribute($id, $val); } - return $xml; } /** @@ -92,21 +100,20 @@ class Xml{ * @param string $id 数字索引key转换为的属性名 */ static public function xml2data(SimpleXMLElement $xml, &$data, $item = 'item', $id = 'id'){ - foreach ($xml as $items) { + foreach ($xml->children() as $items) { $key = $items->getName(); $attr = $items->attributes(); if($key == $item && isset($attr[$id])){ $key = strval($attr[$id]); } - $child = $items->children(); - if(empty($child)){ - $val = strval($items); + if($items->count()){ + self::xml2data($items, $val); } else { - self::xml2data($child, $val); + $val = strval($items); } $data[$key] = $val; } } -} \ No newline at end of file +}