如何将XML数据解析为PHP变量 [英] How to Parse XML data to a PHP variable

查看:138
本文介绍了如何将XML数据解析为PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对php很平庸,对XML一无所知......如果你能详细说明它会帮助我学习。



我试图用PHP编程来执行这个链接的脚本... http://ws.geonames.org/postalCodeSearch ?邮编= VARIABLE_ZIP和放大器;国家= US 。 VARIABLE_ZIP是输入到表单中的实际邮政编码,它将提交上面链接中的信息。该链接的输出创建了一个XML页面,我不想在我的网站上的任何地方显示。



我想要的是将XML数据的纬度和经度值捕获为变量在PHP中并将它们存储到数据库中。

1)我有一个表单

2)用户将他们的邮政编码输入到表单中:

3)在提交表单时:我希望代码捕获由添加了邮政编码变量的链接生成的XML数据: http://ws.geonames.org/postalCodeSearch?postalcode=90210&country=US < a>(我不希望这个页面显示在任何地方,我只想捕获这些数据)

4)我想将这些变量返回到PHP,所以我可以将它们存储在我的数据库中。步骤3对我来说是一个未知的过程。
这是XML代码在单独的页面上生成的内容......(我不希望显示一个页面):

= = = =

 < geonames> 
< totalResultsCount> 1< / totalResultsCount>
< code>< postalcode> 90210< / postalcode>
<名称>比佛利山庄< / name>
< countryCode>美国< / countryCode>
< lat> 34.09011< / lat>
< lng> -118.40648< / lng>
< adminCode1> CA< / adminCode1>
< adminName1>加利福尼亚< / adminName1>
< adminCode2> 037< / adminCode2>
< adminName2>洛杉矶< / adminName2>
< adminCode3 />< adminName3 />
< / code>
< / geonames>

= = = =



有人提供给我以下但我不知道如何执行它并解析我需要的变量:

 <?php 
$ pc = $ _POST ['postalcode'];
$ c = $ _POST ['country'];
$ xml = file_get_contents(http://ws.geonames.org/postalCodeSearch?postalcode={$pc}&country={$c});
file_put_contents(geopost _ {$ pc} _ {$ c} .xml,$ xml);
?>

感谢您的协助!

解决方案

最好的办法是通过直接导航到链接(我使用我自己的邮政编码和国家/地区)来查看XML的结构,然后浏览树状结构以获取所需的数据使用SimpleXMLElement。这个例子抓住纬度和经度:

 <?php 
$ pc = $ _POST ['postalcode'] ;
$ c = $ _POST ['country'];
$ xml = new SimpleXMLElement(file_get_contents(http://ws.geonames.org/postalCodeSearch?postalcode=。$ pc。& country =。$ c));
$ lat = $ xml-> code-> lat;
$ lng = $ xml-> code-> lng;
echoLat:$ lat< br /> Lng:$ lng;
?>

这应该有助于您开始保存这些值。


I am mediocre with php and ignorant with XML...if you can be detailed it will help me learn.

I am attempting to use PHP programming to execute a scripts to this link... http://ws.geonames.org/postalCodeSearch?postalcode=VARIABLE_ZIP&country=US. The VARIABLE_ZIP is the actual zip code entered into the form that will submit the information in the link above. The output of that link creates an XML page that i do not want displayed anywhere on my website.

What I want to do is capture the XML data Latitude and Longitude values as variables within php and store them into a database.

1) I have a form

2) The user inputs their zip code into the form

3) upon submitting the form: I want code to capture the XML data generated by the link with the zip code variable added: http://ws.geonames.org/postalCodeSearch?postalcode=90210&country=US (I don't want this page to display anywhere, I just want to capture the data)

4) I want to take those variables back to PHP so I can store them in my database

*Step 3 is an unknown process for me. This is what the XML code generates on a separate page...(i don't want a page to display):

= = = = =

<geonames>
<totalResultsCount>1</totalResultsCount>
<code><postalcode>90210</postalcode>
<name>Beverly Hills</name>
<countryCode>US</countryCode>
<lat>34.09011</lat>
<lng>-118.40648</lng>
<adminCode1>CA</adminCode1>
<adminName1>California</adminName1>
<adminCode2>037</adminCode2>
<adminName2>Los Angeles</adminName2>
<adminCode3/><adminName3/>
</code>
</geonames>

= = = = =

Someone provided me with the following but I am not sure how to execute it and parse the variables I need:

<?php
$pc = $_POST['postalcode'];
$c = $_POST['country'];
$xml = file_get_contents("http://ws.geonames.org/postalCodeSearch?postalcode={$pc}&country={$c}");
file_put_contents("geopost_{$pc}_{$c}.xml",$xml);
?>

Thank you for your assistance!

解决方案

The best thing to do is to see how the XML is structured by navigating directly to the link (I used my own zipcode and country), and then navigate through the tree grabbing the data you need using SimpleXMLElement. This example grabs the Latitude and Longitude:

<?php
$pc = $_POST['postalcode'];
$c = $_POST['country'];
$xml = new SimpleXMLElement(file_get_contents("http://ws.geonames.org/postalCodeSearch?postalcode=".$pc."&country=".$c));
$lat = $xml->code->lat;
$lng = $xml->code->lng;
echo "Lat: $lat<br/>Lng: $lng";
?>

This should help get you started with storing those values.

这篇关于如何将XML数据解析为PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆