Reputation: 179
Hello guys I am seeking help when it comes to getting text from an XML file with PHP. I am a beginner in PHP and XML and starting to love it anyway straight to the point.
This is the XML that I have:
<?xml version="1.0" encoding="UTF-8"?>
<activity id="1" moduleid="3" modulename="lesson" contextid="22">
<lesson id="1">
<course>2</course>
<name>asdasdasasdasda</name>
<practice>0</practice>
<modattempts>0</modattempts>
<usepassword>0</usepassword>
<password></password>
<dependency>0</dependency>
<conditions>O:8:"stdClass":3:{s:9:"timespent";i:0;s:9:"completed";i:0;s:15:"gradebetterthan";i:0;}</conditions>
<grade>100</grade>
<custom>1</custom>
<ongoing>0</ongoing>
<usemaxgrade>0</usemaxgrade>
<maxanswers>4</maxanswers>
<maxattempts>1</maxattempts>
<review>0</review>
<nextpagedefault>0</nextpagedefault>
<feedback>0</feedback>
<minquestions>0</minquestions>
<maxpages>0</maxpages>
<timed>0</timed>
<maxtime>20</maxtime>
<retake>0</retake>
<activitylink>0</activitylink>
<mediafile>/airline ticket system.txt</mediafile>
<mediaheight>480</mediaheight>
<mediawidth>640</mediawidth>
<mediaclose>0</mediaclose>
<slideshow>0</slideshow>
<width>640</width>
<height>480</height>
<bgcolor>#FFFFFF</bgcolor>
<displayleft>0</displayleft>
<displayleftif>0</displayleftif>
<progressbar>0</progressbar>
<showhighscores>0</showhighscores>
<maxhighscores>10</maxhighscores>
<available>0</available>
<deadline>0</deadline>
<timemodified>1342762739</timemodified>
<pages>
</pages>
<grades>
</grades>
<highscores>
</highscores>
<timers>
</timers>
</lesson>
</activity>
and this is currently my PHP script:
<?php
$data = simplexml_load_file('lesson.xml');
foreach ($data as $dxdata) {
echo "Activity ID: ".$data->activity[0]['id']."<br />";
echo "Module ID: ".$data->activity['moduleid']."<br />";
echo "Module Name: ".$data->activity['modulename']."<br />";
echo "Context ID: ".$data->activity['contextid']."<br />";
echo "Lessons ID: ".$data->lesson[0]['id']."<br />";
echo "Course: ".$data->lesson[0]->course."<br />";
echo "Name: ".$data->lesson[0]->name."<br />";
echo "Practice: ".$data->lesson[0]->practice."<br />";
echo "Modattemps: ".$data->lesson[0]->modattempts."<br />";
echo "usepassword ".$data->lesson[0]->usepassword."<br />";
}
?>
And this is the output of my PHP script:
Activity ID:
Module ID:
Module Name:
Context ID:
Lessons ID: 1
Course: 2
Name: asdasdasasdasda
Practice: 0
Modattemps: 0
usepassword 0
You see the output of my script wasn't able to get the Activity ID, Module ID and Module Name. Those are the ones I'm having difficulty getting from the xml.
I'd been already searching for this in the net hours already can't find the specific example that will help me solve this. And asking here is my last option. Please I will be thankful of all the help provided to me.
Upvotes: 0
Views: 209
Reputation: 51950
Below is an example which takes your XML and outputs the following:
<activity>
attributes (e.g. id="1"
)<lesson>
(there is only one in your example)Note that I've renamed $data
to be $activity
to better match XML element names to PHP variables, and the same with $lesson
.
$activity = simplexml_load_file('lesson.xml');
// Access attributes with array-style syntax
echo "Activity ID: ".$activity['id']."<br />";
echo "Module ID: ".$activity['moduleid']."<br />";
echo "Module Name: ".$activity['modulename']."<br />";
echo "Context ID: ".$activity['contextid']."<br />";
// Loop over each <lesson> element directly under <activity>
foreach ($activity->lesson as $lesson) {
echo "Lessons ID: ".$lesson['id']."<br />";
echo "Course: ".$lesson->course."<br />";
echo "Name: ".$lesson->name."<br />";
echo "Practice: ".$lesson->practice."<br />";
echo "Modattemps: ".$lesson->modattempts."<br />";
echo "usepassword ".$lesson->usepassword."<br />";
}
This outputs the following:
Activity ID: 1
Module ID: 3
Module Name: lesson
Context ID: 22
Lessons ID: 1
Course: 2
Name: asdasdasasdasda
Practice: 0
Modattemps: 0
usepassword 0
For more details on using SimpleXML, see the Basic SimpleXML usage PHP manual page.
Upvotes: 1