0

Possible Duplicate:
PHP: Accessing namespaced XML with SimpleXML

unable to access the object from this array after parsing

<abc>
<ls:location>
      <ls:address1>No 16 GLama</ls:address1>
      <ls:city>Kuala Lumpur</ls:city>
      <ls:zip>58200</ls:zip>
      <ls:latitude>3.092055</ls:latitude>
      <ls:longitude>101.684757</ls:longitude>
</ls:location>
</abc>


how to access to zip object

 $x = new SimpleXmlElement($content);
Community
  • 1
  • 1
Natasha
  • 259
  • 4
  • 11

2 Answers2

1
echo $x->location->zip;
//"58200"

demo: http://codepad.org/FJDSM5su

Esailija
  • 138,174
  • 23
  • 272
  • 326
0
<?php

$content = '<abc>
            <ls:location>
                  <ls:address1>No 16 GLama</ls:address1>
                  <ls:city>Kuala Lumpur</ls:city>
                  <ls:zip>58200</ls:zip>
                  <ls:latitude>3.092055</ls:latitude>
                  <ls:longitude>101.684757</ls:longitude>
            </ls:location>
            </abc>';


$x = new SimpleXmlElement($content);

// accessing the wanted value in the object
// returns zip as an object
$zip = $x->location->zip;

// object to string type conversion
$zip = (string) $zip;

assert('$zip == "58200" /* Expected result: zip = 58200. */');
?>
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141