• XML > JSON Converter
    8 replies, posted
Since the GXML module didn't work somehow, I made an small php script to convert the given url from XML to an JSON string, so you should be able to retrieve it ingame, and get it to an table, its tested online(and works fine afaik), but not ingame yet, so it'd be nice if you could tell me if it works for you. [code] <?php if (!isset($_GET['url'])) { die("You have to set the URL"); }; $content = file_get_contents($_GET['url']); $xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA); $json = json_encode($xml, JSON_PRETTY_PRINT); header("Content-type: text/plain"); echo $json ?> [/code] The code will be "pretty printed", which looks funny if the given XML has long text, and it also works with CData, duh, since some newbs use normal simplexml_load_string and not the "advanced" sheet. I guess the "big server men" already coded an private module so they dont need this, but its for the new kids out there, since I need this myself, only upload the file to an websever(WHICH DOESNT USE DAMN CLOUDFLARE, DUH!) and type in like "http://url/to/file?url=http://url/to/xml" [editline]8th September 2015[/editline] Also, I use the "content type text/plain" header, because pretty print doesnt work (for me atleast) when I use the json header.
[QUOTE=Noi;48640816]Get luaxml from somewhere, it always works.[/QUOTE] LuaXML doesnt support live url though(I saw the module for Lua 5.2)
This is kind of useless as a PHP script, it needs to be a binary module or wrote in pure Lua because a HTTP API isn't really suitable type of thing. It's an order of times slower and requires the bulk of entire web server just to perform the conversion. There's a GMod-compatible XML parser [URL="https://facepunch.com/showthread.php?t=961117"]here[/URL] which you can use. If you're writing a webpage which provides JSON you should be using the [i]application/json[/i] instead as the content type. This should work instead: [lua] local json = util.TableToJSON(XMLToTable([[ <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> ]])) [/lua]
[QUOTE=KillerLUA;48914296]This is kind of useless as a PHP script, it needs to be a binary module or wrote in pure Lua because a HTTP API isn't really suitable type of thing. It's an order of times slower and requires the bulk of entire web server just to perform the conversion. There's a GMod-compatible XML parser [URL="https://facepunch.com/showthread.php?t=961117"]here[/URL] which you can use. If you're writing a webpage which provides JSON you should be using the [i]application/json[/i] instead as the content type. This should work instead: [lua] local json = util.TableToJSON(XMLToTable([[ <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> ]])) [/lua][/QUOTE] You... didnt uhm...read my first post.. "[B]Since the GXML module didn't work somehow[/B]....."
How exactly does it 'not work', do you get any errors and what XML are you trying to parse?
[QUOTE=KillerLUA;48914501]How exactly does it 'not work', do you get any errors and what XML are you trying to parse?[/QUOTE] [url]https://facepunch.com/showthread.php?t=1484331[/url]
Are you installing it to [I]lua/includes/modules[/I]? I'm not sure how the parser would break functions like [I]print[/I] or [I]concommand.Add[/I] when it doesn't reference them, with the one exception being [I]print[/I] on line 613. [editline]16th October 2015[/editline] I just installed it myself and it's working fine, and converting to JSON. If all you want to do is parse XML into Lua then you can remove the [I]util.TableToJSON[/I] call. [t]http://i.imgur.com/xlsSpI5.jpg[/t]
I put it inside /addons/myaddon/lua/includes/modules/gxml.lua :/
Sorry, you need to Log In to post a reply to this thread.