• 技术文章 >PHP >PHP教程

    php xml转换成json的方法

    小妮浅浅小妮浅浅2021-09-02 14:58:41原创178

    1、创建一个PHP示例文件。

    2、找到需要转换的xml文件。

    3、通过“function xmlToArray($xml, $options = array()) {...}”方法将其转换成json即可。

    实例

    function xmlToArray($xml, $options = array()) {
        $defaults = array(
            'namespaceSeparator' => ':',//you may want this to be something other than a colon
            'attributePrefix' => '@',   //to distinguish between attributes and nodes with the same name
            'alwaysArray' => array(),   //array of xml tag names which should always become arrays
            'autoArray' => true,        //only create arrays for tags which appear more than once
            'textContent' => '$',       //key used for the text content of elements
            'autoText' => true,         //skip textContent key if node has no attributes or child nodes
            'keySearch' => false,       //optional search and replace on tag and attribute names
            'keyReplace' => false       //replace values for above search values (as passed to str_replace())
        );
        $options = array_merge($defaults, $options);
        $namespaces = $xml->getDocNamespaces();
        $namespaces[''] = null; //add base (empty) namespace
        //get attributes from all namespaces
        $attributesArray = array();
        foreach ($namespaces as $prefix => $namespace) {
            foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
                //replace characters in attribute name
                if ($options['keySearch']) $attributeName =
                       str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
                $attributeKey = $options['attributePrefix']
                        . ($prefix ? $prefix . $options['namespaceSeparator'] : '')
                        . $attributeName;
                $attributesArray[$attributeKey] = (string)$attribute;
     
            }
     
        }
     
      
     
        //get child nodes from all namespaces
     
        $tagsArray = array();
     
        foreach ($namespaces as $prefix => $namespace) {
     
            foreach ($xml->children($namespace) as $childXml) {
     
                //recurse into child nodes
     
                $childArray = xmlToArray($childXml, $options);
     
                list($childTagName, $childProperties) = each($childArray);
     
      
     
                //replace characters in tag name
     
                if ($options['keySearch']) $childTagName =
     
                        str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
     
                //add namespace prefix, if any
     
                if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
     
      
     
                if (!isset($tagsArray[$childTagName])) {
     
                    //only entry with this key
     
                    //test if tags of this type should always be arrays, no matter the element count
     
                    $tagsArray[$childTagName] =
     
                            in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
     
                            ? array($childProperties) : $childProperties;
     
                } elseif (
     
                    is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
     
                    === range(0, count($tagsArray[$childTagName]) - 1)
     
                ) {
     
                    //key already exists and is integer indexed array
     
                    $tagsArray[$childTagName][] = $childProperties;
     
                } else {
     
                    //key exists so convert to integer indexed array with previous value in position 0
     
                    $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
     
                }
     
            }
     
        }
     
      
     
        //get text content of node
     
        $textContentArray = array();
     
        $plainText = trim((string)$xml);
     
        if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;
     
      
     
        //stick it all together
     
        $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
     
                ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
     
        //return node as array
        return array(
            $xml->getName() => $propertiesArray
        );
    }

    以上就是php xml转换成json的方法,希望对大家有所帮助。更多php学习指路:php教程

    本文转载于php中文网,如有侵犯联系作者修改。

    专题推荐:php xml json
    品易云
    上一篇:php string转换为html的方法 下一篇:PHP中curl_init的使用

    相关文章推荐

    • php如何返回数组最后一个键名• PHP数组元素拼接为字符串• PHP中smart是什么意思• php中int转换日期的方法• php printf函数如何理解• php首字母小写转大写的方法• php数组键名转换为变量名的方法• php根据字数截取替换• Php mbsubstr中文乱码问题的解决• php中调用类的私有方法• php中日期类型的转换• php实现不去重的合并数组• php如何将字符串强制转换为数组• php转换秒数的函数• php string转换为html的方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网