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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:08:27+00:00 2026-05-24T04:08:27+00:00

I am trying to have PHP read an XML file and then convert it

  • 0

I am trying to have PHP read an XML file and then convert it to JSON to use in some classes that I wrote. The problem I am having is it will not loop thru all XML nodes.

For some reason it will only return one node and not both. My guess is maybe my JSON object is not formatted correctly. I have been messing with this for about 2 days, ugh! I am new to this so go easy on me 😉

tracker.xml

<?xml version="1.0" encoding="UTF-8"?>
<tracker>
    <student>
        <id>0425655</id>
        <lname>Doe</lname>
        <fname>John</fname>
    </student>
    <student>
        <id>0123456</id>
        <lname>Smith</lname>
        <fname>Jane</fname>
    </student>
</tracker>

xml.php

class xml
{
    private $path;
    public function __construct($path)
    {
        $this->path = $path; 
    }
    public function xmlParse()
    {
        $xml = simplexml_load_file($this->path);
        return json_encode($xml->children());
    }
}

json.php

class json
{
    private $xmlArray;
    public function __construct($xmlArray)
    {
        $this->xmlArray = $xmlArray;
    }
    public function getJSON()
    {
        $json = json_decode($this->xmlArray);
        foreach($json->student as $v)
        {
            return 'ID: '.$v->id.'Last: '.$v->lname.'First: '.$v->fname;
        } 
    }
}

I know I can pass true as a second parameter to json_decode(), but I wanted to work with objects.

Here’s the output for the json_decode() (after passing it through getJSON for formatting):

{
    "student": [
        {
            "id": "0425655",
            "lname": "Doe",
            "fname": "John"
        },
        {
            "id": "0123456",
            "lname": "Smith",
            "fname": "Jane"
        }
    ]
}
  • 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-05-24T04:08:28+00:00Added an answer on May 24, 2026 at 4:08 am

    return immediately, well, returns from the current function. You want echo for debugging, as in

    foreach($json->student as $v)
    {
        echo 'ID: '.$v->id.'Last: '.$v->lname.'First: '.$v->fname;
    }
    

    If you want to return the result, either just return the JSON object or parse it into an array or string.

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

Sidebar

Related Questions

In php, I'm trying to read a file that is placed above the web
I am trying to read XML elements that have a - in the name.
I have been having some problems trying to get my PHP running. When I
I have a new install of Ubuntu and trying to use the php's mail()
I have a problem with php header redirect. I already spent hours trying to
I have an XML doc that I need to load with PHP. I am
I'm trying to read XML that is being pushed to my java app. I
I am trying to write a script that will read a remote sitemap.xml and
I am trying to read an xml feed that has '-' in the element
So, I have perform two steps a) Create a some random xml file in

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.