XML数据转换为一个PHP数组 [英] Convert XML data to a PHP Array

查看:139
本文介绍了XML数据转换为一个PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个XML文件: http://www.basket.ba/ksbih/ XML / 200_07.xml

I have this XML file: http://www.basket.ba/ksbih/xml/200_07.xml

我用所有主要功能和类XML到阵列的转换,并可能似乎无法得到它propertly工作。

I've used all the major functions and classes for the conversion of XML-to-Array, and couldn't seem to get it work propertly.

其中的一些元素是在所输出的数据丢失时的print_r /的var_dump-ING吧。

Some of the elements were missing in the outputed data when "print_r/var_dump-ing" it.

首先,我因子评分有文件中的语法错误,但事实并非如此。

First, I've tought there was a syntax error in the file, however that was not the case.

我需要一个超级dooper xml2array转换。有一件事可能是有用的:

I need a super-dooper xml2array conversion. One thing might be useful:

如果有一个超级dooper simplexml2array转换脚本/类/函数,这可能有助于更好的,我会减少我目前的code以这种方式,

If there's a super-dooper simplexml2array conversion script/class/function, that might help even better as I will reduce my current code in that way,

推荐答案

你为什么不使用任何类对互联网的?

why don't you use any class out of the internet?

<?php
class xml2array {

    function xml2array($xml) {
        if (is_string($xml)) {
            $this->dom = new DOMDocument;
            $this->dom->loadXml($xml);
        }

        return false;
    }

    function _process($node) { 
        $occurance = array();

        foreach($node->childNodes as $child) {
            $occurance[$child->nodeName]++;
        }

        if($node->nodeType == XML_TEXT_NODE) { 
            $result = html_entity_decode(htmlentities($node->nodeValue, ENT_COMPAT, 'UTF-8'), 
                                     ENT_COMPAT,'ISO-8859-15');
        } 
        else {
            if($node->hasChildNodes()){
                $children = $node->childNodes;

                for($i=0; $i<$children->length; $i++) {
                    $child = $children->item($i);

                    if($child->nodeName != '#text') {
                        if($occurance[$child->nodeName] > 1) {
                            $result[$child->nodeName][] = $this->_process($child);
                        }
                        else {
                            $result[$child->nodeName] = $this->_process($child);
                        }
                    }
                    else if ($child->nodeName == '#text') {
                        $text = $this->_process($child);

                        if (trim($text) != '') {
                            $result[$child->nodeName] = $this->_process($child);
                        }
                    }
                }
            } 

            if($node->hasAttributes()) { 
                $attributes = $node->attributes;

                if(!is_null($attributes)) {
                    foreach ($attributes as $key => $attr) {
                        $result["@".$attr->name] = $attr->value;
                    }
                }
            }
        }

        return $result;
    }

    function getResult() {
        return $this->_process($this->dom);
    }
}

这篇关于XML数据转换为一个PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆