Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Grey Elerson
3,351 PointsReading XML queries in a PHP server-side application
Hey Everybody!
I am working on a project that will create a server-side program that will watch, record, and analyze some data in real-time (or fairly close to it). I've found an API which will provide the information that I'm looking for as a return on a URL-based query.
My problem is this: the information is returned to be in a new tab... and in XML. How would I go about loading the values I need into PHP variables from an XML page? If it helps at all, each query is returned with a list of XML elements, and all of the requested values (what I need) are saved as attributes to those elements. How can I capture those attribute values automatically and have them saved to a mySQL database?
11 Answers
Nick Pettit
Treehouse TeacherHi Grey Elerson,
It sounds like you need a good XML parser for PHP that can convert the data into the format you need. Randy Hoyt might have some good suggestions for you. He's our PHP teacher at Treehouse.
Grey Elerson
3,351 PointsHey Randy Hoyt! Do you have any good XML parsers!? :)
Nick Pettit
Treehouse TeacherI should mention that he's out of the office and won't be back until Monday. In the meantime, Andrew Chalkley or Jim Hoskins might have some suggestions for you.
Andrew Chalkley
Treehouse Guest TeacherI remember dealing with XML in PHP was a pain. It's been a while since I've done it and probably be out dated.
Grey Elerson
3,351 PointsDo you remember what you did to link the two? Or is there another (better) way to parse the XML attribute data?
Andrew Chalkley
Treehouse Guest TeacherI think PHP has a built in XML parser. You need to step over all elements and then use the standard MySQL to insert it in. It's a manual mapping.
Grey Elerson
3,351 PointsI haven't found a good mySQL/PHP introduction yet. I'm working through the database foundations deep dive, but I don't know how to add data from PHP calculation/parse into mySQL. Is there a PHP function for adding data to a database?
Andrew Chalkley
Treehouse Guest TeacherRandy is working on that right now and will be released later this year.
However here's an example from the PHP docs. And if you follow me on Database Foundations you can use the queries in there to insert data.
Randy Hoyt
Treehouse Guest TeacherWe have a good article on parsing XML with PHP on the Treehouse Blog:
That will most likely work for your needs, though you may want to check out the comments there for some alternatives.
Grey Elerson
3,351 PointsThanks guys! That's a lot of help.
Moises Aguilar
Courses Plus Student 8,732 PointsThe blog post that Randy did was really helpful and I was able to parse the XML when I use the single listing, but it is not working when I add in the foreach statement. I have been working on this off and on for awhile and I am just not sure why it is not working.
What I want to do is to bring in a list of events with links to bunch of different pages on my website, currently I am manually inputting the information and it would make my life so much better to be able to automate it.
I would like to eventually import these events into a database and then have them pulled from the database but I figured that I would start one step at a time.
I have pasted in the XML structure
<?xml version="1.0" encoding="utf-8"?>
<EventList ItemCount="2533">
<Event Region="3307191" RegionName="Las Vegas">
<Id>149854</Id>
<Name><![CDATA[Rehab Sundays - DJ Loczi and Shift]]></Name>
<Price Name="MENS Ticket Entry" ProductMessage="" ServiceCharge="5.50">50.00</Price>
<Price Name="LADIES Ticket Entry" ProductMessage="" ServiceCharge="3.30">30.00</Price>
<URL><![CDATA[http://www.wantickets.com/Events/ShowEvent.aspx?eventId=149854&affCode=866b2427436f493d9232]]></URL>
<Date>6/29/2014</Date>
<GMTOffset>-8</GMTOffset>
<StartTime>10:00 AM</StartTime>
<EndTime>6:00 PM</EndTime>
<Promoter ID="9190"><![CDATA[HRHH Hotel/Casino, LLC.]]></Promoter>
<Artists>
<Artist Name="DJ LOCZI" artistID="2296" />
<Artist Name="DJ SHIFT" artistID="2310" />
</Artists>
<Venue><![CDATA[PARADISE BEACH @ HARD ROCK HOTEL & CASINO]]></Venue>
<VenueID>6162</VenueID>
<Address><![CDATA[4455 Paradise Road]]></Address>
<City><![CDATA[Las Vegas]]></City>
<State><![CDATA[NV]]></State>
<ZipCode><![CDATA[89169]]></ZipCode>
<Country><![CDATA[United States]]></Country>
<EventType><![CDATA[Other]]></EventType>
<Talent><![CDATA[]]></Talent>
<Info><![CDATA[<p class="MsoNormal">REHAB Pool Party located at Paradise Beach inside the Hard Rock Hotel & Casino Las Vegas<br /> For cabana reservations please call 702-693-5555 or use our online for to submit request: <a href="http://www.rehablv.com/reservations/">www.rehablv.com/reservations/</a> <br /> Please bring your receipt and ID to the will-call line located at pool entrance.<br /> Will-call begins when the door opens.<br /> <br /> This is a 21 and over event.</p>]]></Info>
<MinimumAge><![CDATA[RESOURCE--;--Events_Ages_21Over]]></MinimumAge>
<ThumbnailImage><![CDATA[http://c308991.r91.cf1.rackcdn.com/SiteFiles/Events/149854/6ddf9a3b-3b61-41ce-8577-ab6373dd79b9.jpeg]]></ThumbnailImage>
<DetailImage><![CDATA[]]></DetailImage>
<FlyerFrontImage><![CDATA[http://c308991.r91.cf1.rackcdn.com/SiteFiles/Events/149854/3d2ee523-3d5d-4748-8381-1f602c56f641.jpg]]></FlyerFrontImage>
<FlyerBackImage><![CDATA[]]></FlyerBackImage>
</Event>
and this is the php code the I am using.
$myevent = simplexml_load_file('http://www.wantickets.com/xml/events.aspx?VenueID=4384&pagesize=10&pagenum=1&affCode=866b2427436f493d9232');
echo '<ul id="eventInfo">';
foreach ($myevent as $eventinfo);
$Name=$eventinfo->Name;
$MenPrice=$eventinfo->Price;
echo "<li><div class="EventName">",$Name,"</div><div class="Price">Men ",'<a href='$URL'>TICKETS</a></li>n';
endforeach;
echo "</ul>";
Any help would be greatly appreciated.