IT

SimpleXMLElement 객체로부터 가치 얻기

lottoking 2020. 5. 15. 08:23
반응형

SimpleXMLElement 객체로부터 가치 얻기


나는 이와 같은 것을 가지고있다 :

$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
);

지명에서 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하고 객체를 반환합니다. 어떻게 가치를 얻을 수 있습니까?


simpleXML 오브젝트를 문자열로 캐스트해야합니다.

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

매직 메소드 __toString ()을 사용할 수도 있습니다.

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

XML 요소의 값이 실수 (위도, 경도, 거리) 인 경우 다음을 사용할 수 있습니다. (float)

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

또한 (int)정수의 경우 :

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

당신이 경우 하지 않는 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


나를 위해 객체보다 배열을 사용하는 것이 더 쉽습니다.

Xml-Object를 변환합니다.

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

you can use the '{}' to access you property, and then you can do as you wish. Save it or display the content.

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

From your example her's the code

        $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'];
               ....

            }     

        }

This is the function that has always helped me convert the xml related values to array

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

    return $out;
}

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

it returns element under current pointer of array, which is 0, so you will get value


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/>';
}

you can convert array with this function

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;
}

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

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

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);

참고URL : https://stackoverflow.com/questions/2867575/get-value-from-simplexmlelement-object

반응형