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
01<?xml version="1.0" encoding="utf-8" ?>
02<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">
03    <External>null</External>
04    <External>nil</External>
05    <Item class="BodyColors">
06        <Properties>
07            <int name="HeadColor">24</int>
08            <int name="LeftArmColor">24</int>
09            <int name="LeftLegColor">24</int>
10            <string name="Name">Body Colors</string>
11            <int name="RightArmColor">24</int>
12            <int name="RightLegColor">24</int>
13            <int name="TorsoColor">1003</int>
14            <bool name="archivable">true</bool>
15        </Properties>
16    </Item>
17</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

01-- `xml` is the string containing XML
02 
03for line in xml:gmatch("[^\n]+") do
04    -- The pattern:
05        -- \n -- newline
06        -- [^\n] -- any character not the newline
07        -- [^\n]+ -- a (longest) sequence of at least 1 non-newline character
08    if line:lower():match("^%s*<int") then
09        -- The pattern:
10            -- ^ -- at the beginning of the line,
11            -- %s* -- any amount of (optional) whitespace
12            -- <int -- The literal characters `int`
13        print(line)
14    end
15end

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
1--<int name=\"..\">...</int> finds every line with this beginning
2--(%w+) returns the value of the name
3--(%d+) returns the value inside the tag
4for key, int in string.gmatch(str, "<int name=\"(%w+)\">(%d+)</int>") do
5    print(key, int);
6end

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