0

可能重复:
PHP:使用 SimpleXML 访问命名空间 XML

解析后无法从此数组访问对象

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


如何访问 zip 对象

 $x = new SimpleXmlElement($content);
4

2 回答 2

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

演示:http ://codepad.org/FJDSM5su

于 2012-07-07T09:32:25.410 回答
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. */');
?>
于 2012-07-07T09:42:12.207 回答