mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
调整xml生成方法,是用PHP内置的SimpleXMLElement对象进行编码
This commit is contained in:
@@ -21,7 +21,6 @@ class Xml{
|
|||||||
'root_attr' => [], //根节点属性
|
'root_attr' => [], //根节点属性
|
||||||
'item_name' => 'item', //数字节点转换的名称
|
'item_name' => 'item', //数字节点转换的名称
|
||||||
'item_key' => 'id', //数字节点转换的属性名
|
'item_key' => 'id', //数字节点转换的属性名
|
||||||
'encoding' => 'utf-8', //字符集编码
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,29 +33,31 @@ class Xml{
|
|||||||
//初始化配置
|
//初始化配置
|
||||||
$config = array_merge($this->config, $config);
|
$config = array_merge($this->config, $config);
|
||||||
|
|
||||||
if(is_array($config['root_attr'] && !empty($config['root_attr']))){
|
//创建XML对象
|
||||||
$attr = [];
|
$xml = new \SimpleXMLElement("<{$config['root_name']}></{$config['root_name']}>");
|
||||||
foreach ($config['root_attr'] as $key => $value) {
|
self::data2xml($xml, $data, $config['item_name'], $config['item_key']);
|
||||||
$attr[] = "{$key}=\"{$value}\"";
|
return $xml->asXML();
|
||||||
}
|
|
||||||
$attr = implode(' ', $attr);
|
|
||||||
}
|
|
||||||
$attr = empty($attr) ? '' : " {$attr}";
|
|
||||||
$xml = "<?xml version=\"1.0\" encoding=\"{$config['encoding']}\"?>";
|
|
||||||
$xml .= "<{$config['root_name']}{$attr}>";
|
|
||||||
$xml .= self::data2xml($data, $config['item_name'], $config['item_key']);
|
|
||||||
$xml .= "</{$config['root_name']}>";
|
|
||||||
return $xml;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
$config = array_merge($this->config, $config);
|
||||||
|
|
||||||
//创建XML对象
|
//创建XML对象
|
||||||
$xml = new SimpleXMLElement($data);
|
$xml = new \SimpleXMLElement($str);
|
||||||
self::xml2data($xml, $data, $config['item_name'], $config['item_key']);
|
if($assoc){
|
||||||
return $data;
|
self::xml2data($xml, $data, $config['item_name'], $config['item_key']);
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,18 +69,25 @@ class Xml{
|
|||||||
* @param string $id 数字索引key转换为的属性名
|
* @param string $id 数字索引key转换为的属性名
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
static public function data2xml($data, $item = 'item', $id = 'id') {
|
static public function data2xml(SimpleXMLElement $xml, $data, $item = 'item', $id = 'id') {
|
||||||
$xml = $attr = '';
|
foreach ($data as $key => $value) {
|
||||||
foreach ($data as $key => $val) {
|
//指定默认的数字key
|
||||||
if(is_numeric($key)){
|
if(is_numeric($key)){
|
||||||
$id && $attr = " {$id}=\"{$key}\"";
|
$id && $val = $key;
|
||||||
$key = $item;
|
$key = $item;
|
||||||
}
|
}
|
||||||
$xml .= "<{$key}{$attr}>";
|
|
||||||
$xml .= (is_array($val) || is_object($val)) ? self::data2xml($val, $item, $id) : $val;
|
//添加子元素
|
||||||
$xml .= "</{$key}>";
|
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转换为的属性名
|
* @param string $id 数字索引key转换为的属性名
|
||||||
*/
|
*/
|
||||||
static public function xml2data(SimpleXMLElement $xml, &$data, $item = 'item', $id = 'id'){
|
static public function xml2data(SimpleXMLElement $xml, &$data, $item = 'item', $id = 'id'){
|
||||||
foreach ($xml as $items) {
|
foreach ($xml->children() as $items) {
|
||||||
$key = $items->getName();
|
$key = $items->getName();
|
||||||
$attr = $items->attributes();
|
$attr = $items->attributes();
|
||||||
if($key == $item && isset($attr[$id])){
|
if($key == $item && isset($attr[$id])){
|
||||||
$key = strval($attr[$id]);
|
$key = strval($attr[$id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$child = $items->children();
|
if($items->count()){
|
||||||
if(empty($child)){
|
self::xml2data($items, $val);
|
||||||
$val = strval($items);
|
|
||||||
} else {
|
} else {
|
||||||
self::xml2data($child, $val);
|
$val = strval($items);
|
||||||
}
|
}
|
||||||
|
|
||||||
$data[$key] = $val;
|
$data[$key] = $val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user