将XML数据转换为PHP数组

时间:2022-10-27 07:38:41

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

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

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.

我已经使用了所有主要的函数和类来转换XML-to-Array,但似乎无法使它在属性上运行。

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

当“print_r / var_dump-ing”时,输出数据中缺少一些元素。

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

首先,我认为文件中存在语法错误,但事实并非如此。

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

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

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,

如果有一个超级dooper simplexml2array转换脚本/类/函数,这可能会有所帮助,因为我会以这种方式减少当前代码,

1 个解决方案

#1


1  

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);
    }
}

#1


1  

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);
    }
}