org\transform\driver\Xml::data2xml PHP Method

data2xml() public static method

数据XML编码
public static data2xml ( SimpleXMLElement $xml, mixed $data, string $item = 'item', string $id = 'id' ) : string
$xml SimpleXMLElement
$data mixed 数据
$item string 数字索引时的节点名称
$id string 数字索引key转换为的属性名
return string
    public static function data2xml(\SimpleXMLElement $xml, $data, $item = 'item', $id = 'id')
    {
        foreach ($data as $key => $value) {
            //指定默认的数字key
            if (is_numeric($key)) {
                $id && ($val = $key);
                $key = $item;
            }
            //添加子元素
            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);
        }
    }