You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
think-library/src/helper/Xmls.php

65 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 5.1 for ThinkPhP 5.1
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\helper;
use DtApp\ThinkLibrary\exception\DtAppException;
/**
* XML管理类
* Class Xmls
* @mixin Xmls
* @package DtApp\ThinkLibrary\helper
*/
class Xmls
{
/**
* 数组转换为xml
* @param array $values 数组
* @return string
* @throws DtAppException
*/
public function toXml($values)
{
if (!is_array($values) || count($values) <= 0) throw new DtAppException('数组数据异常!');
$xml = "<xml>";
foreach ($values as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . $this->toXml($val) . "</" . $key . ">";
} else if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* 将XML转为array
* @param string $xml
* @return mixed
* @throws DtAppException
*/
public function toArray($xml)
{
if (!$xml) throw new DtAppException('xml数据异常');
libxml_disable_entity_loader(true);
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
}