mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-02 13:12:49 +08:00
调整xml生成方法,是用PHP内置的SimpleXMLElement对象进行编码
This commit is contained in:
@@ -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 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;
|
||||
//创建XML对象
|
||||
$xml = new \SimpleXMLElement("<{$config['root_name']}></{$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 .= "</{$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转换为的属性名
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user