XML在php中的数组。没有看到CDATA部分

时间:2017-10-24 21:25:02

标签: php json xml

我有一个我要解析的xml文件。第一部分有params是没问题的。但是从CDATA开始的第二个我无法弄清楚如何将其转换为数组。 我只需要json的分数部分。第一部分是params。我已经尝试过:

$xml = simplexml_load_string('<content>'.$xml.'</content>', null, LIBXML_NOCDATA);

$xml = simplexml_load_file('xmlfile.xml', 'SimpleXMLElement', LIBXML_NOCDATA);

我也试过修剪($ xml),但我无法理解。你能帮助我吗?

XML文件:

<pujs>
<params>
<p id="p0" desc="mean temperature at 2m (°C)" name="t2m" pid="15"     unit="°C" short_desc="mean T" type="4" precision="1"/>
<p id="p1" desc="precipitation accumulation (mm)" name="padavine" pid="26" unit="mm" short_desc="precipitation" type="4" precision="1"/>
<p id="p2" desc="mean wind speed (m/s)" name="veter_hitrost" pid="21" unit="m/s" short_desc="wind speed" type="4" precision="1"/>
<p id="p3" desc="mean global energy radiation (W/m2)" name="energija_gl" pid="27" unit="W/m2" short_desc="global energy" precision="0" type="4"/>
</params>
<![CDATA[
AcademaPUJS.set( { baseurl:"./icons/", gen:"ACADEMA.AEP.getGenerators('archive').genTerminData('yyyy-MM-dd HH:mm')", datatype:"halfhourly", o:[ "p0" , "p1" , "p2" , "p3" ], params:{p0:{ pid:"15", name:"t2m", unit:"°C"},p1:{ pid:"26", name:"padavine", unit:"mm"},p2:{ pid:"21", name:"veter_hitrost", unit:"m/s"},p3:{ pid:"27", name:"energija_gl", unit:"W/m2"}}, points:{_1828:{_113126400:{p0:"-2.1", p1:"0", p2:"0.5", p3:"0"}, _113126410:{}, _113126430:{p0:"-2.2", p1:"0", p2:"0.6", p3:"0"}, _113126440:{}, _113126460:{p0:"-2.3", p1:"0", p2:"1", p3:"0"}, _113126470:{}, _113126490:{p0:"-2.5", p1:"0", p2:"0.8", p3:"0"}, _113126500:{}, _113126520:{p0:"-2.6", p1:"0", p2:"0.8", p3:"0"}, _113126530:{}, _113126550:{p0:"-2.6", p1:"0", p2:"0.7", p3:"0"}, _113126560:{}, _113126580:{p0:"-2.5", p1:"0", p2:"0.9", p3:"0"}, _113126590:{}, _113126610:{p0:"-2.8", p1:"0", p2:"1.3", p3:"0"}, _113126620:{}, _113126640:{p0:"-2.8", p1:"0", p2:"0.8", p3:"0"}, _113126650:{}, _113126670:{p0:"-2.6", p1:"0", p2:"0.8", p3:"0"}, _113126680:{}, _113126700:{p0:"-2.5", p1:"0", p2:"1.6", p3:"0"}, _113126710:{}, _113126730:{p0:"-2.7", p1:"0", p2:"1", p3:"0"}, _113126740:{}, _113126760:{p0:"-2.8", p1:"0", p2:"0.5", p3:"0"}, _113126770:{}, _113126790:{p0:"-2.7", p1:"0", p2:"1", p3:"0"}, _113126800:{}, _113126820:{p0:"-2.8", p1:"0", p2:"0.9", p3:"0"}, _113126830:{}, _113126850:{p0:"-2.8", p1:"0", p2:"0.9", p3:"1"}, _113126860:{}, _113126880:{p0:"-2.8", p1:"0", p2:"0.7", p3:"7"}, _113126890:{}, _113126910:{p0:"-2.8", p1:"0", p2:"1", p3:"14"}, _113126920:{}, _113126940:{p0:"-2.7", p1:"0", p2:"1.3", p3:"37"}, _113126950:{}, _113126970:{p0:"-2.7", p1:"0", p2:"1.2", p3:"54"}, _113126980:{}, _113127000:{p0:"-2.6", p1:"0", p2:"0.7", p3:"67"}, _113127010:{}, _113127030:{p0:"-2.2", p1:"0", p2:"0.9", p3:"136"}, _113127040:{}, _113127060:{p0:"-2", p1:"0", p2:"0.6", p3:"76"}, _113127070:{}, _113127090:{p0:"-1.8", p1:"0.2", p2:"0.7", p3:"82"}, _113127100:{}, _113127120:{p0:"-1.4", p1:"0.2", p2:"0.3", p3:"90"}, _113127130:{}, _113127150:{p0:"-1.3", p1:"0.5", p2:"0.6", p3:"78"}, _113127160:{}, _113127180:{p0:"-1.2", p1:"0.8", p2:"0.6", p3:"77"}, _113127190:{}, _113127210:{p0:"-1.1", p1:"1.3", p2:"0.4", p3:"74"}, _113127220:{}, _113127240:{p0:"-1.1", p1:"1.2", p2:"0.7", p3:"98"}, _113127250:{}, _113127270:{p0:"-0.9", p1:"0.6", p2:"0.3", p3:"92"}, _113127280:{}, _113127300:{p0:"-0.8", p1:"1", p2:"0.6", p3:"75"}, _113127310:{}, _113127330:{p0:"-0.9", p1:"1.3", p2:"0.8", p3:"61"}, _113127340:{}, _113127360:{p0:"-1", p1:"0.6", p2:"0.3", p3:"54"}, _113127370:{}, _113127390:{p0:"-0.8", p1:"0.5", p2:"0.4", p3:"38"}, _113127400:{}, _113127420:{p0:"-0.8", p1:"0.4", p2:"0.4", p3:"12"}, _113127430:{}, _113127450:{p0:"-0.9", p1:"0", p2:"0.6", p3:"0"}, _113127460:{}, _113127480:{p0:"-1", p1:"0", p2:"0.9", p3:"0"}, _113127490:{}, _113127510:{p0:"-1.2", p1:"0", p2:"0.9", p3:"0"}, _113127520:{}, _113127540:{p0:"-1.2", p1:"0.1", p2:"0.4", p3:"0"}, _113127550:{}, _113127570:{p0:"-1.2", p1:"0", p2:"0.3", p3:"0"}, _113127580:{}, _113127600:{p0:"-1", p1:"0", p2:"0.6", p3:"0"}, _113127610:{}, _113127630:{p0:"-1", p1:"0", p2:"0.3", p3:"0"}, _113127640:{}, _113127660:{p0:"-1", p1:"0", p2:"0.3", p3:"0"}, _113127670:{}, _113127690:{p0:"-0.8", p1:"0", p2:"1", p3:"0"}, _113127700:{}, _113127720:{p0:"-0.9", p1:"0", p2:"0.5", p3:"0"}, _113127730:{}, _113127750:{p0:"-0.8", p1:"0", p2:"0.5", p3:"0"}, _113127760:{}, _113127780:{p0:"-0.9", p1:"0", p2:"0.8", p3:"0"}, _113127790:{}, _113127810:{p0:"-0.9", p1:"0", p2:"0.7", p3:"0"}, _113127820:{}, _113127840:{p0:"-0.9", p1:"0", p2:"0.6", p3:"0"}, _113127850:{}, _113127870:{p0:"-0.9", p1:"0", p2:"0.3", p3:"0"}, _113127880:{}, _113127900:{p0:"-0.8", p1:"0", p2:"0.4", p3:"0"}, _113127910:{}, _113127930:{p0:"-0.7", p1:"0", p2:"0.5", p3:"0"}, _113127940:{}, _113127960:{p0:"-0.7", p1:"0", p2:"0.3", p3:"0"}, _113127970:{}, _113127990:{p0:"-0.7", p1:"0", p2:"0.5", p3:"0"}, _113128000:{}, _113128020:{p0:"-0.8", p1:"0", p2:"0.3", p3:"0"}, _113128030:{}, _113128050:{p0:"-0.7", p1:"0", p2:"0.6", p3:"0"}, _113128060:{}, _113128080:{p0:"-0.7", p1:"0", p2:"0.3", p3:"0"}, _113128090:{}, _113128110:{p0:"-0.8", p1:"0", p2:"0.7", p3:"0"}, _113128120:{}, _113128140:{p0:"-0.7", p1:"0", p2:"0.2", p3:"0"}, _113128150:{}, _113128170:{p0:"-0.6", p1:"0", p2:"0.4", p3:"0"}, _113128180:{}, _113128200:{p0:"-0.6", p1:"0", p2:"0.9", p3:"0"}, _113128210:{}, _113128230:{p0:"-0.7", p1:"0", p2:"1.2", p3:"0"}, _113128240:{}, _113128260:{p0:"-0.9", p1:"0", p2:"0.5", p3:"0"}, _113128270:{}, _113128290:{p0:"-1.1", p1:"0", p2:"0.9", p3:"0"}, _113128300:{}, _113128320:{p0:"-0.9", p1:"0", p2:"0.7", p3:"11"}, _113128330:{}, _113128350:{p0:"-0.6", p1:"0", p2:"0.9", p3:"30"}, _113128360:{}, _113128380:{p0:"-0.2", p1:"0", p2:"0.8", p3:"58"}, _113128390:{}, _113128410:{p0:"0.1", p1:"0", p2:"1.4", p3:"80"}, _113128420:{}, _113128440:{p0:"0.2", p1:"0", p2:"1.4", p3:"142"}, _113128450:{}, _113128470:{p0:"0.7", p1:"0.1", p2:"1.7", p3:"224"}, _113128480:{}, _113128500:{p0:"1", p1:"0.2", p2:"1.7", p3:"231"}, _113128510:{}, _113128530:{p0:"0.9", p1:"0.1", p2:"1.6", p3:"190"}, _113128540:{}, _113128560:{p0:"1.1", p1:"0", p2:"1.2", p3:"228"}, _113128570:{}, _113128590:{p0:"1.6", p1:"0.1", p2:"1.4", p3:"258"}, _113128600:{}, _113128620:{p0:"1.7", p1:"0", p2:"1.5", p3:"225"}, _113128630:{}, _113128650:{p0:"1.9", p1:"0", p2:"1.5", p3:"189"}, _113128660:{}, _113128680:{p0:"2", p1:"0", p2:"0.9", p3:"170"}, _113128690:{}, _113128710:{p0:"2.2", p1:"0", p2:"0.3", p3:"160"}, _113128720:{}, _113128740:{p0:"2.5", p1:"0", p2:"0.4", p3:"125"}, _113128750:{}, _113128770:{p0:"2.4", p1:"0", p2:"0.8", p3:"91"}, _113128780:{}, _113128800:{p0:"2.2", p1:"0", p2:"1.8", p3:"44"}, _113128810:{}, _113128830:{p0:"1.7", p1:"0", p2:"1.8", p3:"27"}, _113128840:{}, _113128860:{p0:"1.6", p1:"0", p2:"1.3", p3:"10"}, _113128870:{}, _113128890:{p0:"1.6", p1:"0", p2:"0.6", p3:"1"}, _113128900:{}, _113128920:{p0:"1.6", p1:"0", p2:"1", p3:"0"}, _113128930:{}, _113128950:{p0:"1.4", p1:"0", p2:"0.8", p3:"0"}, _113128960:{}, _113128980:{p0:"1.4", p1:"0", p2:"1.1", p3:"0"}, _113128990:{}, _113129010:{p0:"1.3", p1:"0", p2:"1.6", p3:"0"}, _113129020:{}, _113129040:{p0:"1.2", p1:"0", p2:"1.5", p3:"0"}, _113129050:{}, _113129070:{p0:"1.1", p1:"0.1", p2:"1", p3:"0"}, _113129080:{}, _113129100:{p0:"0.8", p1:"0.3", p2:"1", p3:"0"}, _113129110:{}, _113129130:{p0:"0.5", p1:"0.5", p2:"0.8", p3:"0"}, _113129140:{}, _113129160:{p0:"0.4", p1:"0", p2:"1.2", p3:"0"}, _113129170:{}, _113129190:{p0:"0.3", p1:"0", p2:"2.5", p3:"0"}, _113129200:{}, _113129220:{p0:"0.3", p1:"0", p2:"2.6", p3:"0"}, _113129230:{}, _113129250:{p0:"0.5", p1:"0", p2:"3", p3:"0"}}}})
]]>
</pujs>

有没有人知道如何做到这一点并只获得json的一部分?或者所有转换为数组而不是我会这样做?请帮助我们,我有点迷失在这里......

1 个答案:

答案 0 :(得分:0)

这很糟糕但很脏但是工作

<?php
$xml = file_get_contents('test.xml');
preg_match('@<!\[CDATA\[.+?\..+?\(\s*(.+?)\)\s*\]\]>@s', $xml, $cData);
$cData = preg_replace(array('@(\w+):@','@("HH":mm)@'),array('"\1":','HH:mm'),$cData[1]);
$json = json_decode($cData, true);
var_dump($json);