[英]PHP parsing json from xml file
我对xml和json解析有点熟悉,但是我对此网站有疑问。 我试过了
$json_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml";
$json = file_get_contents($json_string);
$arr=json_decode($json_string, true);
但这不起作用。 当我分析数据时,我发现json中有一些javascript变量,它们在运行javascript时会变成数据,所以也许这就是为什么它不起作用的原因。 不知道你如何解决它。
我想做的是将“ 9:10 CEST”和“ si0_20140930-0710_zm_si.jpg”之类的值解析为php数组。
以下解决方案有效。 不知道是否有更好的方法可以做到:
<?php
$xml_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml";
$xml = file_get_contents($xml_string);
// Extract relevant section out of the file
$start_pos = strpos($xml, "timeline:") + strlen("timeline:");
$end_pos = strpos($xml, "});");
$json = substr($xml, $start_pos, $end_pos - $start_pos);
// Some string replace operations to bind the keys and values within " (double quotes)
$json = preg_replace("/(,[a-z]+)/", '"$1', $json);
$json = preg_replace("/([a-z]+)(:)/", '"$1"$2"', $json);
$json = str_replace('}', '"}', $json);
// echo $json; // This string is now in decodable json format
$arr = json_decode($json, true);
var_dump($arr);
return;
?>
采用 :
$xml=simplexml_load_file("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml");
不要使用json_decode
。
此xml
似乎无效。 它不包含数据规则的任何DTD,甚至不包含标准的<?xml..
标签,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
...
它还包含javascript代码。 SimpleXML均不能解析它(空数组)。
如果可以调用它包含的JavaScript,请执行以下操作:
$data = file_get_contents("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml");
$data = str_replace(array('<?xml version="1.0" encoding="utf-8"?><pujs><![CDATA[',']]></pujs>'),"",$data);
echo '<script type="text/javascript">'.$data."</script>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.