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?