使用 XPATH 读取 XML PHP DOM

发布于 2024-12-13 13:50:49 字数 1915 浏览 0 评论 0原文

我有一个问题。

我有 xml

http://maps.googleapis .com/maps/api/geocode/xml?address=new+york&sensor=true

我想读取示例

GeocodeResponse/result/geometry/location/lat

GeocodeResponse/result/geometry/location/lng

可能使用 XPATH,这就是我到目前为止所拥有的...

<?php

$Address = "new+york";
$Query = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
$XmlResponse = file_get_contents($Query);

$doc = new DOMDocument();
$doc->loadXML($XmlResponse);

$root = $doc->getElementsByTagName( "GeocodeResponse" );
foreach( $root as $val )
{
    $hrefs = $val->getElementsByTagName( "status" );
    $status = $hrefs->item(0)->nodeValue;

    foreach( $hrefs as $val2 )
    {
        $hrefs2 = $val2->getElementsByTagName( "type" );
        $type = $hrefs2->item(0)->nodeValue;

        echo "Type is: $type  <br>";
    }

    echo "Status is: $status  <br>";
}


?>

我可以有一些建议吗?

也许我可以使用

$xpath = new DOMXPath($xml);
$hrefs = $xpath->evaluate("/page");

更新!

我已经设法通过这个得到结果......

$xpath = new DOMXPath($doc);
$res = $xpath->evaluate('//GeocodeResponse/result/geometry');

$root = $doc->getElementsByTagName( "location" );
foreach( $root as $val )
{
    $hrefs = $val->getElementsByTagName( "lat" );
    $status = $hrefs->item(0)->nodeValue;

    echo "Status is: $status  <br>";
}

但我想要一些没有 foreach 的东西,比如

$xpath = new DOMXPath($doc);
$res = $xpath->evaluate('//GeocodeResponse/result/geometry');
$hrefs = $val->getElementsByTagName( "lat" );
$status = $hrefs->item(0)->nodeValue;

echo "Status is: $status  <br>";

这可能吗?

I have a question.

I have the xml

http://maps.googleapis.com/maps/api/geocode/xml?address=new+york&sensor=true

I want to read from example

GeocodeResponse/result/geometry/location/lat

and

GeocodeResponse/result/geometry/location/lng

maybe using XPATH and this is what i have so far ...

<?php

$Address = "new+york";
$Query = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
$XmlResponse = file_get_contents($Query);

$doc = new DOMDocument();
$doc->loadXML($XmlResponse);

$root = $doc->getElementsByTagName( "GeocodeResponse" );
foreach( $root as $val )
{
    $hrefs = $val->getElementsByTagName( "status" );
    $status = $hrefs->item(0)->nodeValue;

    foreach( $hrefs as $val2 )
    {
        $hrefs2 = $val2->getElementsByTagName( "type" );
        $type = $hrefs2->item(0)->nodeValue;

        echo "Type is: $type  <br>";
    }

    echo "Status is: $status  <br>";
}


?>

Can i have some advices?

maybe i can use

$xpath = new DOMXPath($xml);
$hrefs = $xpath->evaluate("/page");

UPDATE!!

I have managed to get the result by this ...

$xpath = new DOMXPath($doc);
$res = $xpath->evaluate('//GeocodeResponse/result/geometry');

$root = $doc->getElementsByTagName( "location" );
foreach( $root as $val )
{
    $hrefs = $val->getElementsByTagName( "lat" );
    $status = $hrefs->item(0)->nodeValue;

    echo "Status is: $status  <br>";
}

but i want something without foreach like

$xpath = new DOMXPath($doc);
$res = $xpath->evaluate('//GeocodeResponse/result/geometry');
$hrefs = $val->getElementsByTagName( "lat" );
$status = $hrefs->item(0)->nodeValue;

echo "Status is: $status  <br>";

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2024-12-20 13:50:49

您可能希望切换到更容易解析的 JSON:

http://maps.googleapis。 com/maps/api/geocode/json?address=new+york&sensor=true

$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=true');

$geocodeResponse = json_decode($json, true);

foreach($geocodeResponse['results'] as $result){
   echo $result['geometry']['location']['lat'].', '.$result['geometry']['location']['lng'] .'<br>';
}

You may want to switch to JSON that is much easier to parse:

http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=true

$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=true');

$geocodeResponse = json_decode($json, true);

foreach($geocodeResponse['results'] as $result){
   echo $result['geometry']['location']['lat'].', '.$result['geometry']['location']['lng'] .'<br>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文