Solved

extracting data from an xml url with php

Posted on 2014-07-22
2
335 Views
Last Modified: 2014-07-22
I am trying to extract three variables from a site that returns an xml file. It contains, among other things, the Latitude, Longitude, CensusTract and CensusBlock.
I am attaching a file that represents what I am trying to do. I just can't figure out the syntax to address the xml data.
xmlpuzle.php
0
Comment
Question by:Cal_George
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40213237
Simple example - you just need to drill down the nodes for any value you want
<?php
$url = 'https://geoservices.tamu.edu/Services/Geocode/WebService/GeocoderWebServiceHttpNonParsed_V04_01.aspx?streetAddress=500%20McMahon%20Road&city=Hollister&state=ca&zip=95023&apikey=demo&format=XML&census=true&censusYear=2010&notStore=false&version=4.01';

$xml=simplexml_load_file($url);
echo "Latitude: ".$xml->OutputGeocodes[0]->OutputGeocode->Latitude."<br>";
echo "Longitude: ".$xml->OutputGeocodes[0]->OutputGeocode->Longitude."<br>";
echo "CensusTract: ".$xml->OutputGeocodes[0]->OutputGeocode->CensusValues->CensusValue->CensusTract."<br>";

Open in new window

0
 
LVL 2

Author Closing Comment

by:Cal_George
ID: 40213339
Well, that was much simpler than what I have been trying all afternoon.
Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question