LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2008, 03:58 PM   #1
lerad512
LQ Newbie
 
Registered: Jul 2006
Posts: 7

Rep: Reputation: 0
Converting XML to JSON in PHP


I'm writing a PHP script to fetch an RSS feed and convert it to JSON, so that I can pass it back to the JavaScript on the client-side. Trouble is, I don't have a lot of experience working with XML, so I was hoping to find a library or a function that could do the conversion for me. I think I found one... but I can't get it to work.

I've seen a couple links to http://www-128.ibm.com/developerwork...x-xml2jsonphp/ (including one from LinuxQuestions, http://www.linuxquestions.org/questi...d.php?t=520798), which has some PHP code that's supposed to do exactly what I want. However, when I include the code in my project, it throws a syntax error. I'm pretty new to PHP - I've done a couple projects, but nothing too large and nothing object-oriented - so I'm not exactly sure where to start. Does anyone have any experience using this code, or another way to convert XML to JSON?

The error looks like this: "Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /xml2json/xml2json.php on line 73". Line 73 is the start of the function. You can see / download the code from the link above, but I'll reproduce the top part of that file here, without all the comments.

<?php
/*...*/
require_once 'json/JSON.php';
// Internal program-specific Debug option.
define ("DEBUG", false);
// Maximum Recursion Depth that we can allow.
define ("MAX_RECURSION_DEPTH_ALLOWED", 25);
// An empty string
define ("EMPTY_STR", "");
// SimpleXMLElement object property name for attributes
define ("SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES", "@attributes");
// SimpleXMLElement object name.
define ("SIMPLE_XML_ELEMENT_PHP_CLASS", "SimpleXMLElement");
class xml2json {
/*...*/
public static function transformXmlStringToJson($xmlStringContents) {

That last line is where it's throwing the syntax error. Any thoughts?
 
Old 05-27-2008, 04:04 PM   #2
lerad512
LQ Newbie
 
Registered: Jul 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Sorry!

Whoops, my bad. Turns out I was using PHP4, and the function would only work in PHP5. I upgraded and now it seems to work just fine.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Converting XML to JSON made easy with PHP LXer Syndicated Linux News 0 01-19-2007 03:03 AM
LXer: How to bridge the gap between XML and Ajax with JSON LXer Syndicated Linux News 0 09-14-2006 08:21 PM
php-json fedora 5 hammer65 Fedora 0 07-15-2006 02:41 PM
XML in PHP Vort3x Linux - Software 1 12-20-2004 09:12 PM
Php Vs. Xml zetsui Programming 4 02-18-2004 12:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration