PHP获取XML所有节点名称

    function readXML($xml_string)
    {
        $dom = new \DOMDocument();
        $dom->loadXML($xml_string);
        $container = [];
        $dom = $this->readXML($xml_string);
        $root = $dom->getElementsByTagName("*");
        foreach ($root as $node) {
            //过滤根节点
            if ($node->nodeName == "xml") {
                continue;
            }
            $container[] = $node->nodeName;
        }
        return $container;
    }

调用

    $xml_string = "<xml>
                        <return_msg><![CDATA[商户不存在]]></return_msg>
                        <version><![CDATA[2.0.0]]></version>
                        <sign_type><![CDATA[MD5]]></sign_type>
                        <return_code><![CDATA[FAIL]]></return_code>
                        <channels>
                            <channel>1001</channel>
                            <channel>1002</channel>
                            <channel>1003</channel>
                        </channels>
                    </xml>";
    var_dump(readXML($xml_string) );
    //output
    /*
        array(8) {
            [0]=>
                string(10) "return_msg"
            [1]=>
                string(7) "version"
            [2]=>
                string(9) "sign_type"
            [3]=>
                string(11) "return_code"
            [4]=>
                string(8) "channels"
            [5]=>
                string(7) "channel"
            [6]=>
                string(7) "channel"
            [7]=>
                string(7) "channel"
        }
     */

转载请注明原文地址:https://blog.keepchen.com/a/php-gets-all-the-xml-node-names.html