如何将 XML 数据解析为 PHP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10586046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:31:43  来源:igfitidea点击:

How to Parse XML data to a PHP variable

phpxmlparsinggeolocation

提问by user1394056

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

我对 php 很平庸,对 XML 一无所知……如果你能详细一点,它将帮助我学习。

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.

我正在尝试使用 PHP 编程来执行此链接的脚本... http://ws.geonames.org/postalCodeSearch?postalcode=VARIABLE_ZIP&country=US。VARIABLE_ZIP 是输入到表单中的实际邮政编码,用于提交上述链接中的信息。该链接的输出创建了一个 XML 页面,我不想在我的网站上的任何地方显示该页面。

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

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

1) I have a form

1)我有一个表格

2) The user inputs their zip code into the form

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

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)

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

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

4)我想将这些变量带回 PHP,以便我可以将它们存储在我的数据库中

*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):

*第 3 步对我来说是一个未知的过程。这是 XML 代码在单独的页面上生成的内容......(我不想显示页面):

= = = = =

= = = = =

<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!

谢谢您的帮助!

回答by maiorano84

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:

最好的做法是通过直接导航到链接(我使用我自己的邮政编码和国家/地区)来查看 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;
echo "Lat: $lat<br/>Lng: $lng";
?>

This should help get you started with storing those values.

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

回答by GDP

Step 3 is to process the xml with PHP which is outlined quite nicely with these Simple XML examples

第 3 步是使用 PHP 处理 xml,这些简单的 XML 示例很好地概述了这一点

回答by h00ligan

Take a look at PHPs SimpleXMLfor how to parse and read the XML data received.

查看 PHPs SimpleXML了解如何解析和读取收到的 XML 数据。