如何将XML解析为JSON数组?

时间:2017-02-21 18:21:54

标签: php xml xml-parsing

如何将XML解析为JSON数组?

2 个答案:

答案 0 :(得分:0)

我找到了这个解决方案:

if (!function_exists('xmlToArray')) {
    function xmlToArray($xmlInput, $bodyXpath) {
        $response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xmlInput);
        $xmlTest = new SimpleXMLElement($response);
        $body = $xmlTest - > xpath('//soapenvBody')[0];
        $simpleXml = json_decode(json_encode((array) $body), true);
        $test = simplexml_load_string($simpleXml['qdeinvokeResponse']['qdereturn']['ns1data']);
        return json_decode(json_encode((array) $test), true);
    }
}

答案 1 :(得分:0)

https://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

或者

简单地说, $json = json_encode(simplexml_load_string($xmlString)); $array = json_decode($json,true);