Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:XML到JSON的转换失败_Php_Json_Xml - Fatal编程技术网

PHP:XML到JSON的转换失败

PHP:XML到JSON的转换失败,php,json,xml,Php,Json,Xml,我使用以下函数将XML转换为Json: function XMLtoJSON($xml) { $xml = file_get_contents($xml); // gets XML content from file $xml = str_replace(array("\n", "\r", "\t"), '', $xml); // removes newlines, returns and tabs //

我使用以下函数将XML转换为Json:

        function XMLtoJSON($xml) {
          $xml = file_get_contents($xml);    // gets XML content from file
          $xml = str_replace(array("\n", "\r", "\t"), '', $xml);    // removes newlines, returns and tabs

          // replace double quotes with single quotes, to ensure the simple XML function can parse the XML
          $xml = trim(str_replace('"', "'", $xml));
          $simpleXml = simplexml_load_string($xml);

          return stripslashes(json_encode($simpleXml));    // returns a string with JSON object
        }   
我正在尝试转换以下XML文件:

所以我的代码是:

$myVar1 = XMLtoJSON("https://www.comdinheiro.com.br/temp/COMDINHEIRO_GabrielVilella1146066400.xml");

var_dump($myVar1);
以下是var_转储结果:

string(455) "{"controle":{"data_hora":"2017-05-12 20:26:47","usuario":"GabrielVilella","ferramenta":"ExtratoCarteira016"},"variaveis":{"nome_portfolio":"ApelidoTodos","data_ini":"2015-05-05","data_fim":"2016-08-12","cas_dec_q":"2","cas_dec_v":"2","discrimina_fundos":"0","discrimina_fundos_cvm":"0","discrimina_ativos":"1","discrimina_cadgeral":"0","data_fim_agenda":"2017-06-05","filtro_eventos":"todos","filtro_tipo_ativo":"todos","flag_export":"xml"},"resposta":{}}"

转换工作正常。但是,它无法填充“resposta”的元素:{}。未显示任何错误消息。所以我问:为什么“resposta”:{}是空的?

可能是因为p0和p1元素上的名称空间(tab),如果添加
$xml=str_replace('注意:simplexml可以处理换行符、制表符和双引号)。如果必须删除它们,系统上的某些东西确实会被破坏。