Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to print certain elements of an asset's XML?

<?xml version="1.0" encoding="utf-8" ?>
<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.roblox.com/roblox.xsd" version="4">
    <External>null</External>
    <External>nil</External>
    <Item class="BodyColors">
        <Properties>
            <int name="HeadColor">24</int>
            <int name="LeftArmColor">24</int>
            <int name="LeftLegColor">24</int>
            <string name="Name">Body Colors</string>
            <int name="RightArmColor">24</int>
            <int name="RightLegColor">24</int>
            <int name="TorsoColor">1003</int>
            <bool name="archivable">true</bool>
        </Properties>
    </Item>
</roblox>

I need a string manipulation method to print every line containing the int tag.

2 answers

✓ Accepted answer

A full XML parser is a large task. But, we can probably assume certain things about format -- for instance, that each tag is on its own line.

In that case, it's pretty easy to split lines up using :gmatch

-- `xml` is the string containing XML

for line in xml:gmatch("[^\n]+") do
	-- The pattern:
		-- \n -- newline
		-- [^\n] -- any character not the newline
		-- [^\n]+ -- a (longest) sequence of at least 1 non-newline character
	if line:lower():match("^%s*<int") then
		-- The pattern:
			-- ^ -- at the beginning of the line,
			-- %s* -- any amount of (optional) whitespace
			-- <int -- The literal characters `int`
		print(line)
	end
end

EDIT: Thank you PiggyJingles, I wrote "\s" when I meant to write "%s" (\s is used by JavaScript; %s by Lua)

1 comment

1

I think you meant to put a "%" instead of a "" on the 8th line.

--<int name=\"..\">...</int> finds every line with this beginning
--(%w+) returns the value of the name
--(%d+) returns the value inside the tag
for key, int in string.gmatch(str, "<int name=\"(%w+)\">(%d+)</int>") do
	print(key, int);
end

key is the name, int is the integer in the middle of the tags

1 comment

0

This is a really beautiful solution (except the inherent ugliness of regex)

This discussion is preserved as it was. New answers, comments, and votes are closed.