Link to home
Start Free TrialLog in
Avatar of jrlitm
jrlitm

asked on

Parsing XML using XMLReader in PHP

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
Avatar of Roonaan
Roonaan
Flag of Netherlands image

$xmlFile = 'myxml.xml';

$xmlObj = simplexml_load_file($xmlFile);

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

echo $firstInstitutionName;
Avatar of jrlitm
jrlitm

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial