Its been five(5) months now since we started parsing huge(500mb) XML files, the tasks were challenging because of the different format of feeds that we need to unify. We have designed a general parser that can return an array version of the feed but sometimes it fails because of unusual XML structure. Its good thing to have a general parser, but if you would like to start learning how to parse XML in PHP, use SimpleXML.
Save the code below as sample.xml
<Employees>
<Employee id="033-1998-0371" Year="1998">
<Name name="Jay-ar Bauson" age="21" />
<Course>Bachelor of Science in Computer Science</Course>
</Employee>
<Employee id="245-2008-1830" Year="2009">
<Name name="John Doe" age="26" />
<Course>Bachelor of Science in Information Technology</Course>
</Employee>
</Employees>
and save this as parser.php
<?php
$xml = simplexml_load_file('sample.xml');
foreach($xml as $row){
echo "ID: ".$row->attributes()->id."<br/>";
echo "Year: ".$row->attributes()->Year."<br/>";
echo "Name: ".$row->Name->attributes()->name."<br/>";
echo "Age: ".$row->Name->attributes()->age."<br/>";
echo "Course: ".$row->Course."<hr/>";
}
?>
Executing parser.php from your browser will result something like:
ID: 033-1998-0371
Year: 1998
Name: Jay-ar Bauson
Age: 21
Course: Bachelor of Science in Computer Science
______________________________________________________
ID: 245-2008-1830
Year: 2009
Name: John Doe
Age: 26
Course: Bachelor of Science in Information Technology
______________________________________________________

Recent Comments