Jump to content

Looping through xml with php.


Solarpitch

Recommended Posts

Hey,

 

I have the following code that gets the name of the course from the XML.

 

XML Snippet

<?xml version="1.0"?>
<courses>
  <course>
    <id>70</id>
    <name>Marketing and Social Media</name>
    <dates>
      <date>
        <instance_id>6747</instance_id>
        <location />
        <course_type>Day</course_type>
        <date_available>2011/07/13</date_available>
        <time_start>9:30</time_start>
        <time_end>17:00</time_end>
        <availibility>7</availibility>
        <running_dates>
          <running_date>2011/07/13</running_date>
          <running_date>2011/07/14</running_date>
        </running_dates>
      </date>
    </dates>
  </course>
  <course>
</courses>

 

PHP

$dom = new DomDocument();
$dom->load("courses.xml");
$xp = new domxpath($dom);

$titles = $xp->query("/courses/course/name");
foreach ($titles as $node) {

    print $node->textContent . " ";

}

 

This prints out the course name fine. My question is how Can i get the rest of the data from the same query. For example why cant I do something like..

 

...
foreach ($titles as $node) {

    print $node->name. " ";
    print $node->time_start. " ";
    print $node->course_type. " ";

etc..

}

 

So I want to loop through get the name and assign the rest of the nodes in there to variables or something.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.