2014-09-30 3 views
1

xml 및 json 파싱에 다소 familliar이지만이 사이트에 문제가 있습니다. 시도했지만xml 파일에서 json을 파싱하는 PHP

$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 내부의 자바 스크립트 변수가 있다는 것을 알았습니다. 그대를 고칠 방법을 고르지 마라.

내가하고 싶은 것은 "9:10 CEST"와 같은 값을 구문 분석하고 ... "si0_20140930-0710_zm_si.jpg"... PHP 배열에 넣는 것입니다.

+1

해독하려는 JSON이 아니라 XML이다. 대신에'simplexml'을 사용하십시오. –

+0

파일은 xml입니다. 그렇다면 왜 json_decode()를 사용하고 있습니까? – Khushboo

+0

xml eather와 함께 작동하지 않습니다 – Jakadinho

답변

1

다음 솔루션은 작동합니다. 더 좋은 방법이 있는지 모르겠다.

<?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; 
?> 
+0

이것은 내가 찾고있는 것입니다 ... 조금 수정했고 이제는 완벽합니다! 고마워. – Jakadinho

0

사용 :

$xml=simplexml_load_file("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml"); 

json_decode를 사용하지 않습니다.

+0

나는 $ url = file_get_contents ("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml")을 시도했다. $ xml = simplexml_load_string ($ url); print_r ($ xml); 하지만 그것은 작동하지 않습니다. SimpleXMLElement Object() – Jakadinho

0

xml은 유효하지 않은 것 같습니다. 그것은이 같은 데이터 규칙 또는 표준 <?xml.. 태그에 대한 일체의 DTD 포함되어 있지 않습니다

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE note SYSTEM "Note.dtd"> 
... 

자바 스크립트 코드를 또한 포함되어 있습니다. SimpleXML은 파싱 할 수 없습니다 (빈 배열). 당신이 보유하고있는 자바 스크립트를 호출 할 경우

다음을 수행하십시오

$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>"; 
관련 문제