Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 10 years ago
<?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

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
I think you meant to put a "%" instead of a "\" on the 8th line. PiggyJingles 358 — 10y
Ad
Log in to vote
2
Answered by 10 years ago
--<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

0
This is a really beautiful solution (except the inherent ugliness of regex) BlueTaslem 18071 — 10y

Answer this question