获得从SimpleXMLElement对象值获得从SimpleXMLElement对象值(Get v

2019-05-10 11:00发布

我有这样的事情:

$url = "http://ws.geonames.org/findNearbyPostalCodes?country=pl&placename=";
$url .= rawurlencode($city[$i]);

$xml = simplexml_load_file($url);
echo $url."\n";
$cityCode[] = array(
    'city' => $city[$i], 
    'lat' => $xml->code[0]->lat, 
    'lng' => $xml->code[0]->lng
);

它应该从GEONAMES下载XML。 如果我做print_r($xml)我得到:

SimpleXMLElement Object
(
    [code] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [postalcode] => 01-935
                    [name] => Warszawa
                    [countryCode] => PL
                    [lat] => 52.25
                    [lng] => 21.0
                    [adminCode1] => SimpleXMLElement Object
                        (
                        )

                    [adminName1] => Mazowieckie
                    [adminCode2] => SimpleXMLElement Object
                        (
                        )

                    [adminName2] => Warszawa
                    [adminCode3] => SimpleXMLElement Object
                        (
                        )

                    [adminName3] => SimpleXMLElement Object
                        (
                        )

                    [distance] => 0.0
                )

我这样做,你可以看到$xml->code[0]->lat ,它返回一个对象。 我怎样才能获得的价值?

Answer 1:

你要投的SimpleXML对象为一个字符串。

$value = (string) $xml->code[0]->lat;


Answer 2:

您也可以使用魔术方法__toString()

$xml->code[0]->lat->__toString()


Answer 3:

如果您知道XML元素的值是一个浮点数(纬度,经度,距离),你可以使用(float)

$value = (float) $xml->code[0]->lat;

另外, (int)为整数:

$value = (int) $xml->code[0]->distance;


Answer 4:

对我来说,它更容易使用数组不是对象,

所以,我的转换XML的对象,

$xml = simplexml_load_file('xml_file.xml');    
$json_string = json_encode($xml);    
$result_array = json_decode($json_string, TRUE);


Answer 5:

如果你知道XML元素的值,你可以使用

$value = (string) $xml->code[0]->lat;

if (ctype_digit($value)) {
    // the value is probably an integer because consists only of digits
}

它的工作原理,当你需要确定是否值是一个数字,因为(string)将始终返回字符串和is_int($value)返回false



Answer 6:

您可以使用“{}”来访问你的财产,然后如你所愿,你可以做。 保存或显示内容。

    $varName = $xml->{'key'};

从您的例子她是代码

        $filePath = __DIR__ . 'Your path ';
        $fileName = 'YourFilename.xml';

        if (file_exists($filePath . $fileName)) {
            $xml = simplexml_load_file($filePath . $fileName);
            $mainNode = $xml->{'code'};

            $cityArray = array();

            foreach ($mainNode as $key => $data)        {
               $cityArray[..] = $mainNode[$key]['cityCode'];
               ....

            }     

        }


Answer 7:

这是一个一直帮助我将XML相关数值数组功能

function _xml2array ( $xmlObject, $out = array () ){
    foreach ( (array) $xmlObject as $index => $node )
        $out[$index] = ( is_object ( $node ) ) ? _xml2array ( $node ) : $node;

    return $out;
}


Answer 8:

尝试current($xml->code[0]->lat)

它阵列,其是0的当前指针下返回元件,所以你会得到值



Answer 9:

header("Content-Type: text/html; charset=utf8");
$url  = simplexml_load_file("http://URI.com");

 foreach ($url->PRODUCT as $product) {  
    foreach($urun->attributes() as $k => $v) {
        echo $k." : ".$v.' <br />';
    }
    echo '<hr/>';
}


Answer 10:

你可以转换阵列使用此功能

function xml2array($xml){
$arr = array();

foreach ($xml->children() as $r)
{
    $t = array();
    if(count($r->children()) == 0)
    {
        $arr[$r->getName()] = strval($r);
    }
    else
    {
        $arr[$r->getName()][] = xml2array($r);
    }
}
return $arr;
}


Answer 11:

$codeZero = null;
foreach ($xml->code->children() as $child) {
   $codeZero = $child;
}

$lat = null;
foreach ($codeZero->children() as $child) {
   if (isset($child->lat)) {
      $lat = $child->lat;
   }
}


Answer 12:

foreach($xml->code as $vals )
{ 
    unset($geonames);
    $vals=(array)$vals;
    foreach($vals as $key => $value)
      {
        $value=(array)$value;
        $geonames[$key]=$value[0];
      }
}
print_r($geonames);


文章来源: Get value from SimpleXMLElement Object
标签: php simplexml