Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

extracting data from an xml url with php

Posted on 2014-07-22
2
Medium Priority
?
366 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

885 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