简体   繁体   English

PHP 从 XML 检索数据

[英]PHP Retrieve DATA from XML

My first attempt at retrieving data from XML for a maps application has failed.我第一次尝试从 XML 检索地图应用程序的数据失败了。 Here is a piece of the XML Feed.这是一块 XML Feed。

<?xml version="1.0" encoding="UTF-8"?>
<DirectionsResponse>
 <status>OK</status>
 <route>
  <leg>

   <start_address>Winkfield, Bracknell, Berkshire RG42 6LY, UK</start_address>
   <end_address>Wentworth, Surrey GU25 4, UK</end_address>

  </leg>
 </route>
</DirectionsResponse>

I want to get the start and end address and return them via AJAX to the application.我想获取开始和结束地址并通过 AJAX 将它们返回给应用程序。

The PHP PHP

<?php 

$start = $_POST['start'];
$end = $_POST['end'];

$xml = simplexml_load_file('http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$end.'&sensor=false');


// data to fetch

$start = $xml->xpath("/DirectionsResponse/route/leg/start_address");

$end = $xml->xpath("/DirectionsResponse/route/leg/end_address");

$start = array($start);
// output

echo json_encode( array('output'=>$start[0]));


?>

Annoyingly this is returning an object to the page.令人讨厌的是,这会将 object 返回到页面。

Response:: {"output":[{"0":"Winkfield, Windsor, Berkshire SL4 2ES, UK"}]}响应:: {"output":[{"0":"Winkfield, Windsor, Berkshire SL4 2ES, UK"}]}

Anyone know how to stop that from happening.任何人都知道如何阻止这种情况发生。 I just want the value Winkfield, Windsor, Berkshire SL4 2ES, UK.我只想要价值 Winkfield, Windsor, Berkshire SL4 2ES, UK。

Haven't tested your specific case, but i remember running into something similar when using SimpleXML, you might want to use (string) to cast it out of the object尚未测试您的具体情况,但我记得在使用 SimpleXML 时遇到类似的情况,您可能想使用 (string) 将其从 object

array('output'=> (string)$start[0])

Or rather just leave out $start = array($start) and just do或者更确切地说只是省略 $start = array($start) 而只是做

array('output'=> (string)$start)

On reading the SimpleXML XPath documentation (http://www.php.net/manual/en/simplexmlelement.xpath.php) again i think your problem might be this:在再次阅读 SimpleXML XPath 文档(http://www.php.net/manual/en/simplexmlelement.xpath.php)时,我认为您的问题可能是这样的:

Returns an array of SimpleXMLElement objects or FALSE in case of an error.

So the XPath returns an array, then you wrap that in an array and take the first element of that array, so all you end up with is the original array - remove the array wrap and you should be fine所以 XPath 返回一个数组,然后你将它包装在一个数组中并获取该数组的第一个元素,所以你最终得到的是原始数组 - 删除数组包装,你应该没问题

function XMLReader()
{
    $MyArray = array();

    $doc = new DOMDocument(); 
    $doc->load( 'XMLFilePath.xml' ); 
    $info = $doc->getElementsByTagName( "leg" );
    foreach( $info as $Type )
    {
        $details = $Type->getElementsByTagName( "start_address" );
        $detail = $details->item(0)->nodeValue;     
        $MyArray [] = $detail; 
    }

    return $MyArray;
}

and same for end_Address.和 end_Address 相同。 I wish this answer is helpful.我希望这个答案是有帮助的。

echo $xml->route->leg->start_address;

This is the edited answer.这是编辑后的答案。 I checked it.我检查了它。 Its working properly on the XML.它在 XML 上正常工作。

Just echo $start?只是回声 $start? Also, why do you make an array of start and then output the first element, it doesnt make any sense at all.另外,为什么要创建一个数组 start 然后 output 是第一个元素,这根本没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM