Solved

Parsing XML using XMLReader in PHP

Posted on 2008-06-20
3
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

691 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