将XML存储在Array中

时间:2013-03-23 06:37:02

标签: php javascript xml arrays

这里是我要将xml转换为数组的链接 http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000
点击上面的链接返回一个XML文件
我想把它存放在阵列上 请帮忙...
谢谢

3 个答案:

答案 0 :(得分:2)

您可以使用simplexml_load_string

php

对于示例

<?php
$string = <<<XML
<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

$xml = simplexml_load_string($string);

print_r($xml);
?>

<强>输出

SimpleXMLElement Object
(
  [title] => Forty What?
  [from] => Joe
  [to] => Jane
  [body] =>
   I know that's the answer -- but what's the question?
)

阅读http://php.net/manual/en/function.simplexml-load-string.php了解详情。

您也可以使用https://github.com/gaarf/XML-string-to-PHP-array/blob/master/xmlstr_to_array.php

答案 1 :(得分:2)

只是一个小小的注释..转换为数组很容易作为强制转换(数组)

$xml = file_get_contents('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000', true);
$xml = (array)simplexml_load_string($xml);
print_r($xml);

答案 2 :(得分:1)

您必须获取它,然后转换为简单的xml对象。

$responce = file_get_contents('http://services.gisgraphy.com/geoloc/search?lat=22.298569900000000000&lng=70.794301799999970000&radius=7000', true); print_r(simplexml_load_string($responce));