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 7783243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:46:47+00:00 2026-06-01T19:46:47+00:00

Here is an xml example that I load from an external xml file $data

  • 0

Here is an xml example that I load from an external xml file
$data = file_get_contents($filename);
$dom->loadXML($data);

Using php, I want to walk through this xml to get the title, question, and each question’s choices.

<questions>
    <record topic = "classic video games">
        <title>Centipede</title> 
        <question>How many shots does it take to destroy a mushroom?</question> 
        <choices>
            <choice correct="no" votes="0">1</choice>
            <choice correct="no" votes="0">2</choice>
            <choice correct="no" votes="0">3</choice>
            <choice correct="yes" votes="0">4</choice>
        </choices>
    </record>
    <record topic = "classic video games">
        <title>Quake</title>
        <question>What is the name of the most powerful weapon in Quake?</question>
        <choices>
            <choice correct="no" votes="0">gauntlet</choice>
            <choice correct="no" votes="0">machine gun</choice>
            <choice correct="yes" votes="0">BFG2000</choice>
            <choice correct="no" votes="0">rocket launcher</choice>
            <choice correct="no" votes="0">railgun</choice>
        </choices>
    </record>
</questions>

I want my result to be like this:

Title = Centipede

Question = How many shots does it take to destroy a mushroom?

I. 1
II. 2
III. 3
IV. 4

here is my try:

$dom->loadXML($data);


    $all_records = $dom->getElementsByTagName("record");
        $all_choices = $dom->getElementsByTagName("choices"); 
        foreach($all_records as $record){
        $question = $record->getElementsByTagName("question")->item(0)->nodeValue;
        $title = $record->getElementsByTagName("title")->item(0)->nodeValue;
        echo "<h2> Title=$title</h2>";
                echo "<h5><em>Question=$question</em></h5>";
                echo "<li>". $all_choices->getElementsByTagName("choice")->nodeValue."</li>\n";

        echo "</ul>\n";
        echo "</div>\n";

    }

I’m able to get the title and the question, but not the choices!

  • 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-01T19:46:49+00:00Added an answer on June 1, 2026 at 7:46 pm

    You need to fetch the choices set using an inner loop as you inspect each record:

    <?php
    
    $dom->loadXML($data);
    
    $all_records = $dom->getElementsByTagName("record");
    foreach($all_records as $record) {
        $question = $record->getElementsByTagName("question")->item(0)->nodeValue;
        $title = $record->getElementsByTagName("title")->item(0)->nodeValue;
        $choices = $record->getElementsByTagName('choice');
    
        echo "<h2> Title=$title</h2>";
        echo "<h5><em>Question=$question</em></h5>";
    
        echo '<ol class="choices">';
        foreach ( $choices as $choice ) {
            echo "<li>" . $choice->nodeValue . "</li>\n";
        }
        echo "</ol>";
    
        echo "</div>\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that reads a series of tasks from an XML file
I'm trying to load an XML file from a separate project; One of these
I've seen several posts here on SO about loading XML documents from some data
Why is twitter double encoding XML entity references? Here's an example tweet: xml entity
here is my xml DOM. <entities> <entity id=7006276> <title>xxx</title> <entities> <entity id=7877579> <title>xxx</title> <entities>
here is the XML file : <com.google.android.maps.MapView android:layout_width=fill_parent android:layout_height=fill_parent android:apiKey=0q7NUYm4bgzeXlqXtKYVPJDRWUJmt8Cu0gvbWMx android:id=@+id/map_view /> and Manifest
here is the xml data : <root> <recommendedMaterials> <value label=Aluminium data=0 /> <value label=Iron
I'm developing a simple application that fetches some data from wowarmory.com. What I need
I have created a PHP script which builds a table from an XML document
I just tested an early PowerShell WPF example from here #requires -version 2 Add-Type

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.