Solved

Parsing XML using XMLReader in PHP

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 76
how to use jquery variable in mysql query 20 74
Load XML element 3 43
CRM 2011 Advanced Find Producing Error 3 41
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

734 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