1

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"
 }
 ]
}
asked Jul 23, 2011 at 17:42
18
  • Your class is poorly-named. So is your question title. Commented Jul 23, 2011 at 17:48
  • What's wrong with the JSON output? It looks fine to me. You should explain what output you're looking for, and how it differs from what you're getting. Commented Jul 23, 2011 at 17:48
  • +1 For posting the code (albeit only snippets, instead of like 5 consecutive lines. However, you forgot to tell us the exact problem ;) . What output are you getting in what step, and what are you expecting? Commented Jul 23, 2011 at 17:48
  • Both student nodes are in that JSON output, they're in an array in the student key of the object. Commented Jul 23, 2011 at 17:51
  • 1
    @user738910: We know what you're doing. Excel is not relevant and just muddies the issue. Your question was OK as it was. You should read the answers, which explain the problem. Commented Jul 23, 2011 at 17:55

3 Answers 3

1

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.

answered Jul 23, 2011 at 17:49
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I now see what was going on. I only was looking at the comments under my post and failed to look further down this page to see all of what you guys have wrote. I owe all you an appology for getting frustrated. Sorry and thank you!
0

That JSON string looks right to me, problem lies with the return statement - as you're looping through an array, use echo instead.

answered Jul 23, 2011 at 17:50

Comments

0

Regarding your JSON

I reformatted your output. As you can see, there are two nodes under "student". Perhaps you missed the [ and ] characters.

Format your JSON next time to get a better idea of what's going on. :)


Regarding your function

You also might have missed it because your debug output is broken:

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

You return after the first iteration.

Try:

$output = '';
foreach ($json->student as $v) {
 $output .= "ID: {$v->id} Last: {$v->lname} First: {$v->fname}\n";
} 
return $output;

To be honest, though, I'd expect a function named getJSON() to return, well... JSON. Not some author-written string. Your classes and functions are poorly named overall.

Perhaps your function should look like this:

public function getJSON()
{
 $json = json_decode($this->xmlArray);
 // some debug output for development
 foreach ($json->student as $v) {
 echo "ID: {$v->id} Last: {$v->lname} First: {$v->fname}\n";
 }
 return $json;
}
answered Jul 23, 2011 at 17:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.