我正在使用远程xml,我必须解析为json以将信息推送到移动应用程序。 我正在使用json_encode。
我的问题是,对于特定节点,有时它是一个数组,有时候它是一个字典。我需要定位该特定节点并使其始终为数组。
当我的应用程序是字典而不是数组时会崩溃,所以我必须得到[...]。
我的xml看起来像:
<infogeneral>
<idspotwebcamhd>6</idspotwebcamhd>
<spot>Anglet Sables d'Or</spot>
<url/>
<vagues/>
<vent>GFS</vent>
<ventdate>2016-04-12 06:00:00</ventdate>
<venttimestamp>1460433600</venttimestamp>
<houle>WW3</houle>
<houledate>2016-04-12 06:00:00</houledate>
<houletimestamp>1460433600</houletimestamp>
<tempeaucelsius>13</tempeaucelsius>
</infogeneral>
<day>
<date>Tue 12 Apr 2016</date>
<datetimestamp>1460484000</datetimestamp>
<firstlight>06:55</firstlight>
<lastlight>21:17</lastlight>
<creneau>
<heure>20.00</heure>
<timestamp>1460484000</timestamp>
<ventnoeud>9</ventnoeud>
<ventms/>
<ventkmh>17</ventkmh>
<ventbft/>
<ventmph/>
<houlemetre>1.10</houlemetre>
<houlepied/>
<periode>12</periode>
<temp>15</temp>
<ventdirection>so</ventdirection>
<houledirection>ono</houledirection>
<pictogramme>3cloud_lightrain.png</pictogramme>
</creneau>
</day>
<day>
<date>Wed 13 Apr 2016</date>
<datetimestamp>1460505600</datetimestamp>
<firstlight>06:53</firstlight>
<lastlight>21:18</lastlight>
<principal>
<heure>14:00</heure>
<pictogrammeprincipal>1cloud_norain.png</pictogrammeprincipal>
<tempmincelsius>13</tempmincelsius>
<tempmaxcelsius>17</tempmaxcelsius>
</principal>
<creneau>
<heure>08.00</heure>
<timestamp>1460527200</timestamp>
<ventnoeud>17</ventnoeud>
<ventms/>
<ventkmh>32</ventkmh>
<ventbft/>
<ventmph/>
<houlemetre>1.20</houlemetre>
<houlepied/>
<periode>10</periode>
<temp>13</temp>
<ventdirection>sso</ventdirection>
<houledirection>ono</houledirection>
<pictogramme>4cloud_norain.png</pictogramme>
</creneau>
<creneau>
<heure>11.00</heure>
<timestamp>1460538000</timestamp>
<ventnoeud>19</ventnoeud>
<ventms/>
<ventkmh>36</ventkmh>
<ventbft/>
<ventmph/>
<houlemetre>1.40</houlemetre>
<houlepied/>
<periode>6</periode>
<temp>15</temp>
<ventdirection>so</ventdirection>
<houledirection>so</houledirection>
<pictogramme>2cloud_norain.png</pictogramme>
</creneau>
<creneau>
<heure>14.00</heure>
<timestamp>1460548800</timestamp>
<ventnoeud>16</ventnoeud>
<ventms/>
<ventkmh>29</ventkmh>
<ventbft/>
<ventmph/>
<houlemetre>1.30</houlemetre>
<houlepied/>
<periode>6</periode>
<temp>17</temp>
<ventdirection>oso</ventdirection>
<houledirection>so</houledirection>
<pictogramme>1cloud_norain.png</pictogramme>
</creneau>
<creneau>
<heure>17.00</heure>
<timestamp>1460559600</timestamp>
<ventnoeud>7</ventnoeud>
<ventms/>
<ventkmh>13</ventkmh>
<ventbft/>
<ventmph/>
<houlemetre>1.00</houlemetre>
<houlepied/>
<periode>5</periode>
<temp>17</temp>
<ventdirection>no</ventdirection>
<houledirection>oso</houledirection>
<pictogramme>0cloud.png</pictogramme>
</creneau>
<creneau>
<heure>20.00</heure>
<timestamp>1460570400</timestamp>
<ventnoeud>4</ventnoeud>
<ventms/>
<ventkmh>7</ventkmh>
<ventbft/>
<ventmph/>
<houlemetre>0.80</houlemetre>
<houlepied/>
<periode>10</periode>
<temp>15</temp>
<ventdirection>ne</ventdirection>
<houledirection>ono</houledirection>
<pictogramme>0cloud.png</pictogramme>
</creneau>
</day>
我的php将xml转换为json(我加载一个xml文件来保存json文件):
$xml = simplexml_load_file( $in_filepath );
$json = json_encode($xml);
file_put_contents ( $out_filepath, $json );
对于我在制作前的测试,我的php看起来像:
$xml = simplexml_load_file('http://www.example.com/myFile.xml');
$json = json_encode($xml);
print_r($json);
在json_encode之后:
"infogeneral":{
"idspotwebcamhd":"6",
"spot":"Anglet Sables d'Or",
"url":{
},
"vagues":{
},
"vent":"GFS",
"ventdate":"2016-04-12 06:00:00",
"venttimestamp":"1460433600",
"houle":"WW3",
"houledate":"2016-04-12 06:00:00",
"houletimestamp":"1460433600",
"tempeaucelsius":"13"
},
"day":[
{
"date":"Tue 12 Apr 2016",
"datetimestamp":"1460484000",
"firstlight":"06:55",
"lastlight":"21:17",
"creneau":{
"heure":"20.00",
"timestamp":"1460484000",
"ventnoeud":"9",
"ventms":{
},
"ventkmh":"17",
"ventbft":{
},
"ventmph":{
},
"houlemetre":"1.10",
"houlepied":{
},
"periode":"12",
"temp":"15",
"ventdirection":"so",
"houledirection":"ono",
"pictogramme":"3cloud_lightrain.png"
}
},
{
"date":"Wed 13 Apr 2016",
"datetimestamp":"1460505600",
"firstlight":"06:53",
"lastlight":"21:18",
"principal":{
"heure":"14:00",
"pictogrammeprincipal":"1cloud_norain.png",
"tempmincelsius":"13",
"tempmaxcelsius":"17"
},
"creneau":[
{
"heure":"08.00",
"timestamp":"1460527200",
"ventnoeud":"17",
"ventms":{
},
"ventkmh":"32",
"ventbft":{
},
"ventmph":{
},
"houlemetre":"1.20",
"houlepied":{
},
"periode":"10",
"temp":"13",
"ventdirection":"sso",
"houledirection":"ono",
"pictogramme":"4cloud_norain.png"
},
{
"heure":"11.00",
"timestamp":"1460538000",
"ventnoeud":"19",
"ventms":{
},
"ventkmh":"36",
"ventbft":{
},
"ventmph":{
},
"houlemetre":"1.40",
"houlepied":{
},
"periode":"6",
"temp":"15",
"ventdirection":"so",
"houledirection":"so",
"pictogramme":"2cloud_norain.png"
},
{
"heure":"14.00",
"timestamp":"1460548800",
"ventnoeud":"16",
"ventms":{
},
"ventkmh":"29",
"ventbft":{
},
"ventmph":{
},
"houlemetre":"1.30",
"houlepied":{
},
"periode":"6",
"temp":"17",
"ventdirection":"oso",
"houledirection":"so",
"pictogramme":"1cloud_norain.png"
},
{
"heure":"17.00",
"timestamp":"1460559600",
"ventnoeud":"7",
"ventms":{
},
"ventkmh":"13",
"ventbft":{
},
"ventmph":{
},
"houlemetre":"1.00",
"houlepied":{
},
"periode":"5",
"temp":"17",
"ventdirection":"no",
"houledirection":"oso",
"pictogramme":"0cloud.png"
},
{
"heure":"20.00",
"timestamp":"1460570400",
"ventnoeud":"4",
"ventms":{
},
"ventkmh":"7",
"ventbft":{
},
"ventmph":{
},
"houlemetre":"0.80",
"houlepied":{
},
"periode":"10",
"temp":"15",
"ventdirection":"ne",
"houledirection":"ono",
"pictogramme":"0cloud.png"
}
]
},
第一天我只有一个&#34; creneau&#34;所以它崩溃了。
我试过了:http://www.binarytides.com/convert-simplexml-object-to-array-in-php/
但它会改变整棵树。
关于如何完成这项工作的任何想法?
答案 0 :(得分:0)
我认为SimpleXMLElement::count
和SimpleXMLElement::xpath
的组合可以帮助您识别单个条目并添加另一个假条目。我认为这是一种不好的做法,但如果您无法访问移动应用程序,这可能是一种解决方法,如果空条目不会产生任何副作用。
但为了实现一个干净的解决方案,我建议修改移动应用程序中的代码。
$xml = SimpleXMLElement();
// get your data into $xml here
$creneaus = $xml->xpath('/infogeneral/day/creneau');
foreach ($creneaus as $creneau) {
if ( $creneau->count() == 1 ) {
$parent = $creneau->xpath( '..' );
$creneauEmpty = simplexml_load_string('<creneau><heure>...</creneau>');
$parent->addChild($creneauEmpty);
}
}