Solved

Parsing XML using XMLReader in PHP

Posted on 2008-06-20
3
444 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now