gxml - A XML parser

Ever wanted to parse that steam community XML data or maybe some RSS feed?
Well, here you go:


What does it do?

It parses XML!

Download

Put in garrysmod/lua/includes/modules (not lua/bin)

Credits to original creator for making this awesome piece of code.

Example:



require'gxml'

PrintTable(XMLToTable[[
<note>
	<to>Tove</to>
	<from>Jani</from>
	<heading>Reminder</heading>
	<body>Don't forget me this weekend!</body>
</note>	
]])
-- result:
{
	["note"] = {
		["to"] = "Tove",
		["body"] = "Don't forget me this weekend!",
		["heading"] = "Reminder",
		["from"] = "Jani",
	},
}



Notes
This doesn’t work in all cases, it doesn’t do everything for you. Hell, it might not even work or just error.

Hell yeah, been waiting for this a long time.
Thanks!

Hmm, I could see some good use like up to date clan or community information.

This should’ve been out when I wrote that script to prevent Serenity-users from joining.

No need to check if the ‘gxml’ table already exists as require already does that.

The example?

First of all it is just an example and I wanted error if the require failed. You could throw the second “if” into the first “if” for useless optimization though.

I’m saying that theres no need to do:
[lua]
if not gxml then require “gxml” end
[/lua]
because require already checks if the table exists.

Ah, true. I wasn’t thinking. Doesn’t really harm though :stuck_out_tongue:

In fact, it was made before Serenity “became public”, but I sort of forgot to release it :eek:

This looks useful, rated tool, thanks!

Haha nice :smile:

you stole my idea

[editline]01:29PM[/editline]

EDIT: so i deserve atleast a litle bit of the credit

XML is just XML. That XML module is just parsing XML.

You can use [Http.Get

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Http.Get) to get the community data if you really need it.
Then use the XMLToTable helper function above to parse what you got from the website.
To get the xml version of the community pages just append ?xml=true on the URL. Example: http://steamcommunity.com/id/zoe?xml=true

My “:Community()” helper is not really polished, yet. I would have released it otherwise.

Sorry about his bump guys but this needs moving to module releases.
And also, this is amazing work!

Binary Modules (dlls) this is a lua module so I disagree and rated you dumb

Well i got it working now.

But i still have a problem, how would i go about changing a steamid into a community id.

[lua]function GetComID(IDIn)
local stSteamID = string.Explode(":", IDIn)
if (#stSteamID == 3) then
unsigned long long iCommunityID = tonumber(stSteamID[3]) * 2 + 76561197960265728 + tonumber(stSteamID[2])
Msg(iCommunityID)
end[/lua]

Returning this.

[lua]] lua_run GetComID(“STEAM_0:1:20264839”)
> GetComID(“STEAM_0:1:20264839”)…
7.6561198000795e+016[/lua]

Theres gotta be a working way of doing this.

[editline]9th April 2011[/editline]

Well even if so it needs to go to the requests section.

Why would it need to be in the request section

I had a look at this but it didnt work.

I kept getting this error.


Running script xml.lua...
[lua\xml.lua:18] '=' expected near 'long'


[editline]9th April 2011[/editline]

Please take this over to the requests/questions section, dont want to take up this nice thread.

one second. I’lll find the implementation I use.

Thank you very much.

[lua]

– AbitraryPrecision by someone :s

AbitraryPrecision = {}
function AbitraryPrecision.__MakeTable(v)
local v = tostring(v)
local t = {}
for k=1,v:len() do
t[k] = v:sub(k,k)
end
return t
end
function AbitraryPrecision.__MakeString(v)
local len = #v
local str = “”
while(len > 0) do
str = str…v[len]
len = len - 1
end
return str
end
function AbitraryPrecision.Add(a,b)
local A = AbitraryPrecision.__MakeTable(a)
local B = AbitraryPrecision.__MakeTable(b)

local pos1 = #A
local pos2 = #B
local pos3 = 1
local C = {}
local overflow = 0
while(pos1 &gt; 0 or pos2 &gt; 0) do
	local digit = (A[pos1] or 0) + (B[pos2] or 0) + overflow
	overflow = 0
	if(digit &gt; 9) then
		overflow = 1
		digit = digit - 10
	end
	C[pos3] = digit
	pos1 = pos1 - 1
	pos2 = pos2 - 1
	pos3 = pos3 + 1
end
if(overflow == 1) then
	C[pos3] = 1
end

return AbitraryPrecision.__MakeString(C)

end
function AbitraryPrecision.Sub(a,b)
local A = AbitraryPrecision.__MakeTable(a)
local B = AbitraryPrecision.__MakeTable(b)

local pos1 = #A
local pos2 = #B
local pos3 = 1
local C = {}
local overflow = 0
while(pos1 &gt; 0 or pos2 &gt; 0) do
	local digit = (A[pos1] or 0) - (B[pos2] or 0) - overflow
	overflow = 0
	if(digit &lt; 0) then
		overflow = 1
		digit = digit + 10
	end
	C[pos3] = digit
	pos1 = pos1 - 1
	pos2 = pos2 - 1
	pos3 = pos3 + 1
end
if(overflow == 1) then
	C[pos3] = 1
end

return AbitraryPrecision.__MakeString(C)

end



– SteamID <-> Community ID

local OFFSET = “76561197960265728”

SteamID64 = SteamID64 or function(steamid)
local data = string.Explode(":",steamid)

if !data[3] || !data[2] then return false end

return AbitraryPrecision.Add(data[2] + 2*data[3],OFFSET) --  A + 2*B + OFFSET

end
util.SteamID64=SteamID64

SteamIDString = SteamIDString or function(steamid64)
if type(steamid64) != “number” || steamid64 < 76561197960265728 then return false end

local id = AbitraryPrecision.Sub(steamid64,OFFSET) --  A + 2*B
local A = (id % 2)
local B = (id - A)/2

return "STEAM_0:"..A..":"..B

end

util.SteamIDString=SteamIDString

[/lua]
Thanks, whoever made it.