NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Parsing XML in PHP with SimpleXML

Wednesday, July 9th, 2008

As I have been working on a number of social site integration APIs lately, XML has become my best friend. I have used a number of different parsing APIs, but I must say, PHP's SimpleXML is by far the best iteration-based XML parser I have seen to date, and the main reason... It is strongly typed, without having to code a countless number of lines in a custom library. I'll leave the explanation to an example:

We want to parse the following XML file:
<names>
<name>
<first>Tom</first>
<last>Hearn</last>
</name>
<name>
<first>Thomas</first>
<last>Hearn</last>
</name>
</names>

To do so in SimpleXml, all you have to do is the following:

$xml = simplexml_load_string($inputfromabove);

foreach ($names as $name) {
echo $name->first . " " . $name->last;
}

And that's it!

Why is this so cool? Well mainly because this parser doesn't require you to create classes to do strongly-typed XML iteration. All you have to do is load the string, and it automatically creates the objects from there. Pretty sweet hey?

XML Tom Out.

Tags

PHP

Related Blogs

Resolving CURLOPT_FOLLOWLOCATION cannot be activated with PHP and CURL
Netbeans and PHP: A promising future
Implementing AJAX with PHP and MySQL
Calling .NET WebService From PHP
Simple encryption class for PHP

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations