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
https://forums.phpfreaks.com/topic/237548-looping-through-xml-with-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.