douhe2305 2014-08-29 22:54
浏览 66
已采纳

在PHP中读取XML的一个节点

I have been searching for how to read one node of XML in PHP. The PHP documentation wasn't helpful because I don't understand how to use PHP. All of the tutorials I found weren't useful beacause I only need PHP to read XML(I use CSHTML for Databases and other server-side things). I have working code that can read XML as a tree if it is in a RSS format. I am trying to get the google map geocode api information, from "http://maps.googleapis.com/maps/api/geocode/xml?latlng=38.7876639,-90.8455276&sensor=false." I only want the very first "Formatted_address" node. My current code is;

<?php
$xml=("http://maps.googleapis.com/maps/api/geocode/xml?latlng=38.7876639,-90.8455276&sensor=false");

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

//get and output "<result>" elements
$x=$xmlDoc->getElementsByTagName('result');
for ($i=0; $i<=2; $i++)
  {
  $item=$x->result($i)->getElementsByTagName('formatted_address')
  ->result(0)->childNodes->result(0)->nodeValue;

  echo ( $item);
  }
?>

However this always returns a 500 error and I don't understand what i am doing wrong. Thank you all in advance.

  • 写回答

1条回答 默认 最新

  • dtdr84101 2014-08-29 23:00
    关注

    Change result to item

    $item = $x->item($i)->getElementsByTagName('formatted_address')
              ->item(0)->childNodes->result(0)->nodeValue;
    

    As you can see DOMNodeList only has one method called item(int $index)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效