duanchuopi2298 2017-03-23 09:16
浏览 95

将XML转换为具有属性名称的数组

There are noumerous questions on converting XML to array, and simpliest method is as follows:

$data = unserialize(serialize(json_decode(json_encode((array)simplexml_load_string($dataXml)), 1)));

However, I cant get attributes names using this approach, and could not find any questions on SO, explaining how to do it.

Here is part of XML that I have:

<Object>
                <attribute name="Surname">Ярош</attribute>
                <attribute name="Name">Анна</attribute>
                <attribute name="Middle name">Григорьевна</attribute>
                <attribute name="Position">Торговый представитель розничных продаж</attribute>
                <attribute name="City">BAIKALSEA Company Иркутск</attribute>
                <attribute name="Division">Отдел продаж</attribute>
                <attribute name="Department">Продажи</attribute>
                <attribute name="Email">yarosh@baikalsea.com</attribute>
                <attribute name="MobPhone">79149274726</attribute>
                <attribute name="WorkPhone">-</attribute>
                <attribute name="Manager">Нет</attribute>
                <attribute name="HonoredWorker">Нет</attribute>
                <attribute name="Login">yarosh@baikalsea.com</attribute>
                <attribute name="Character">Пользователь</attribute>
        </Object>

And here is what I get converting it with encode/decode and simpleXml:

enter image description here

As you can see 'name' attribute and its value is lost. I need those name attributes and their values as well. Pls any help appreciated.

  • 写回答

2条回答 默认 最新

  • douzhanglun4482 2017-03-23 10:18
    关注

    Ok, I finally found function, that converts XML to object, preserving namespaces, attributes, childs and so on:

    function xmlObjToArr($obj) { 
            $namespace = $obj->getDocNamespaces(true); 
            $namespace[NULL] = NULL; 
    
            $children = array(); 
            $attributes = array(); 
            $name = strtolower((string)$obj->getName()); 
    
            $text = trim((string)$obj); 
            if( strlen($text) <= 0 ) { 
                $text = NULL; 
            } 
    
            // get info for all namespaces 
            if(is_object($obj)) { 
                foreach( $namespace as $ns=>$nsUrl ) { 
                    // atributes 
                    $objAttributes = $obj->attributes($ns, true); 
                    foreach( $objAttributes as $attributeName => $attributeValue ) { 
                        $attribName = strtolower(trim((string)$attributeName)); 
                        $attribVal = trim((string)$attributeValue); 
                        if (!empty($ns)) { 
                            $attribName = $ns . ':' . $attribName; 
                        } 
                        $attributes[$attribName] = $attribVal; 
                    } 
    
                    // children 
                    $objChildren = $obj->children($ns, true); 
                    foreach( $objChildren as $childName=>$child ) { 
                        $childName = strtolower((string)$childName); 
                        if( !empty($ns) ) { 
                            $childName = $ns.':'.$childName; 
                        } 
                        $children[$childName][] = xmlObjToArr($child); 
                    } 
                } 
            } 
    
            return array( 
                'name'=>$name, 
                'text'=>$text, 
                'attributes'=>$attributes, 
                'children'=>$children 
            ); 
        } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题