【问题标题】:convert xml data to php array将xml数据转换为php数组
【发布时间】:2016-04-05 11:30:49
【问题描述】:

我有带有原始数据的 xml 行列名,我需要将其转换为所需的数组形式:

    <RETS ReplyCode="0" ReplyText="Operation Success.">
<DELIMITER value="09"/>
<COLUMNS>
        AcresNum
        ADAFeaturesYN
        AdditionalFeatures
    </COLUMNS>
<DATA>
     0.0000 0   Ceiling Fan,Granite Counters,Multiple Attics,Security System</DATA></RETS>

我需要将它转换成这样的数组形式:

array( [0] => array( ['AcresNum'] => 0.0000 , 
                     ['ADAFeaturesYN'] => 0),
       [1] => array( ['AcresNum'] => 1.0000 , 
                     ['ADAFeaturesYN'] => 1)  

     );

【问题讨论】:

  • 如果xml是字符串,你可以使用simplexml_load_string(php.net/manual/en/function.simplexml-load-string.php),如果你是xml文件,你可以使用simplexml_load_file(php.net/manual/en/function.simplexml-load-file.php)
  • 我试过了,但返回的是这样的 Array ( [COLUMNS => AcresNum ADAFeaturesYN AdditionalFeatures AdditionalInformation )
  • 处理Array ( [COLUMNS =&gt; AcresNum ADAFeaturesYN AdditionalFeatures AdditionalInformation) 以创建所需的数组
  • 我投票决定将此问题作为题外话结束,因为它 - 以目前的形式 - 是一个“给我写代码”的问题。

标签: php mysql arrays xml


【解决方案1】:

您可以使用simplexml_load_file

$file = 'test.xml';
if (file_exists($file)) {
    $xml = simplexml_load_file($file);
    print_r($xml);
} else {
    exit('Failed to open '.$file);
}

【讨论】:

    【解决方案2】:

    这里有一组函数,可以递归地将任何 XML 转换为数组,无论深度和复杂性如何。包括标签,属性等,你的名字...... 请注意,任何属性标签都以@attributes 作为键设置在数组中,但可以是您想要的任何东西。 在你的情况下使用

      $result=xmlstr_to_array($xmlstring);
    

    它被转换为一个数组,随心所欲地操作你的东西。

        // load xml string
        function xmlstr_to_array($xmlstr) {
            $doc = new DOMDocument();
            $doc->loadXML($xmlstr);
            return domnode_to_array($doc->documentElement);
        }
    
        // convert nodes
        function domnode_to_array($node) {
        $output = array();
        switch ($node->nodeType) {
            case XML_CDATA_SECTION_NODE:
            case XML_TEXT_NODE:
                $output = trim($node->textContent);
                break;
            case XML_ELEMENT_NODE:
                for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
                    $child = $node->childNodes->item($i);
                    $v = domnode_to_array($child);
                    if (isset($child->tagName)) {
                        $t = ConvertTypes($child->tagName);
                        if (!isset($output[$t])) {
                            $output[$t] = array();
                        }
                        $output[$t][] = $v;
                    } elseif ($v) {
                        $output = (string) $v;
                    }
                }
                if (is_array($output)) {
                    if ($node->attributes->length) {
                        $a = array();
                        foreach ($node->attributes as $attrName => $attrNode) {
                            $a[$attrName] = ConvertTypes($attrNode->value);
                        }
                        $output['@attributes'] = $a;
                    }
                    foreach ($output as $t => $v) {
                        if (is_array($v) && count($v) == 1 && $t != '@attributes') {
                            $output[$t] = $v[0];
                        }
                    }
                }
                break;
        }
        return $output;
    }
    
        //convert types
        function ConvertTypes($org) {
        if (is_numeric($org)) {
            $val = floatval($org);
        } else {
            if ($org === 'true') {
                $val = true;
            } else if ($org === 'false') {
                $val = false;
            } else {
                if ($org === '') {
                    $val = null;
                } else {
                    $val = $org;
                }
            }
        }
        return $val;
    }
    

    【讨论】:

    • 我已经实现了,但我有这样的列 Array ([COLUMNS => AcresNum ADAFeaturesYN AdditionalFeatures AdditionalInformation)
    • 在这种情况下,首先在数组中转换,然后将列元素分解为多个。
    • 这更像是一个数组函数,而不是一个xml问题。在解析 xml 时这样做太棘手了。拆分进程。
    • 我试过用explode()来分割值,但它不会发生
    • 一旦你将xml作为一个数组,我看不出有什么问题要爆炸并用数组替换元素。
    猜你喜欢
    • 1970-01-01
    • 2011-12-25
    • 2015-09-03
    • 2016-10-03
    • 1970-01-01
    • 2017-06-24
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多