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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:58:48+00:00 2026-06-02T08:58:48+00:00

I am trying to read this XML file using PHP and I have two

  • 0

I am trying to read this XML file using PHP and I have two root elements. The code that I wrote in PHP reads only one root element and when I add the other one (<action>) it gives me an error.
I want to do something like this : if($xml->action=="register") then print all parameters.

This is my XML file:

<?xml version='1.0' encoding='ISO-8859-1'?>
<action>register</action>
<paramters>
    <name>Johnny B</name>
    <username>John</username>    
</paramters>

And this is my PHP script:

<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
{
    echo $child->getName() . ": " . $child . "<br />";
}
?>

I really don’t know how to do all this…

  • 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-02T08:58:49+00:00Added an answer on June 2, 2026 at 8:58 am

    Fix your XML, it’s invalid. XML files can only have 1 root element.

    Example valid XML:

    <?xml version='1.0' encoding='ISO-8859-1'?>
    <action>
        <type>register</type>
        <name>Johnny B</name>
        <username>John</username>
    </actions>
    

    Or if you want only parameters to have own elements:

    <?xml version='1.0' encoding='ISO-8859-1'?>
    <action type="register">
        <name>Johnny B</name>
        <username>John</username>
    </actions>
    

    or if you want multiple actions:

    <?xml version='1.0' encoding='ISO-8859-1'?>
    <actions>
        <action type="register">
            <name>Johnny B</name>
            <username>John</username>
        </action>
    </actions>
    

    EDIT:

    As I’ve said in my comment, your teacher should fix his XML. It is invalid. Also he should put his XML through a validator.

    If you’re really desperate you can introduce an articificial root element, but this is really bad practice and should be avoided at all costs:

    $xmlstring = str_replace(
        array('<action>','</paramters>'),
        array('<root><action>', '</paramters></root>'),
        $xmlstring
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read an XML file using LINQ. I have had no
I am trying to read a value from an XML file using LINQ. This
I am trying to read the XML Documentation file (C#) using this ocde -
Does play automatically cache data? I'm trying to read this code that someone else
I have this error when I am trying to read the file: Exception in
I'm trying to read the iTunes XML file with PHP. I want to extract
In php, I'm trying to read a file that is placed above the web
Hi I am trying to read XML File using XLinq and binding the values
Hello I am trying to read my xml document using xpath. I have been
Hi there I have been trying to read an xml file with UTF strings,

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.