Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8291223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:03:09+00:00 2026-06-08T13:03:09+00:00

Hello guys I am seeking help when it comes to getting text from an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:03:12+00:00Added an answer on June 8, 2026 at 1:03 pm

    Below is an example which takes your XML and outputs the following:

    • details from the <activity> attributes (e.g. id="1")
    • lesson details for each <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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys for example when I open with notepad a text file it's shows
hello guys i need small help in understanding file system of android Now in
hello guys i badly need your help, php noob here, im creating a bulletin
Hello guys I'm trying to sending my JSONArray to php page I'm getting HTTP//1.1
Hello guys. I think it isn't possible just using PHP, but just to be
hello guys I am trying to extract all the anchor links from aol but
Hello guys i try to get an object global/persistent over more than one php
Hello guys can anyone help me with the issue im having with installation of
Hello guys i'm having one hell of a time trying to parse an xml
Hello Guys! I am going to write website crawler, which starts from root address

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.