Solved

Parsing XML using XMLReader in PHP

Posted on 2008-06-20
3
446 Views
Last Modified: 2011-09-20
I am having great difficulties finding any resources on how to parse a pretty simple XML script.  I have tried both simpleXML and the XMLReader tools and just cannot seem to pull the data out of the Arrays correctly.  

Here is the XML code:

<stations>
    <institution>
        <name>Franklin</name>  
        <station_name>ADAC_SCU</station_name>
        <station_name>AXIS467</station_name>
        <station_name>Discovery80842</station_name>
        <station_name>FRK_US_1</station_name>
    </institution>
    <institution>                      
        <name>Arapahoe</name>
        <station_name>ARAPUS</station_name>    
    </institution>    
    <institution>        
        <name>East</name>
        <station_name>EASTASPN</station_name>    
    </institution>        
</stations>

Any assistance on how to pull this data out into a php web page and then parse it out?

Thanks
JRLITM
0
Comment
Question by:jrlitm
  • 2
3 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 21832509
$xmlFile = 'myxml.xml';

$xmlObj = simplexml_load_file($xmlFile);

$firstInstitutionName = (String) $xmlObj->insitution[0]->name;

echo $firstInstitutionName;
0
 

Author Comment

by:jrlitm
ID: 21832676
Thanks Roonaan.

Using this example I see the data in $xmlObj however a null is passed into $firstInstitutionName and shows empty

Do you have an example of how to pull these out using a foreach  so that  I have an array like the following.

 institution  = "Franklin"
        station[0]  = "ADAC_SCU"
        station[1]  = "AXIS467"
        station[2]  = "Discovery80842"
        station[3]  = "FRK_US_1"


Thanks
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 125 total points
ID: 21832721
The null is because I misspelled institution. Try this:

<?php

$xmlFile = 'myxml.xml';

$xmlObj = simplexml_load_file($xmlFile);

foreach($xmlObj->institution as $institution) {
      echo $institution->name. '<br />';
      foreach($institution->station_name as $station_name) {
            echo '- '.$station_name . '<br />';
      }
}
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

825 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