使用 XPATH 读取 XML PHP DOM
我有一个问题。
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能希望切换到更容易解析的 JSON:
http://maps.googleapis。 com/maps/api/geocode/json?address=new+york&sensor=true
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