A intuitive XMLParser entirely implemented in LuaU
Parses XML strings into similar AST
Also Parses AST back into XML
- Prologs
- Tags
- Singles
- Comments
Roblox Console
-- Run in Roblox Studio ConsolelocalHttpService=game:GetService("HttpService");
localReplicatedStorage=game:GetService("ReplicatedStorage");
localLastValue=HttpService.HttpEnabledHttpService.HttpEnabled=truelocalBase="https://raw.githubusercontent.com/Perthys/xml-parser/main/src/"localModules= { "init", "Entities", "Node", "Parser", "Serializer" }
localXMLModule=Instance.new("ModuleScript");
XMLModule.Name="XML";
for_, Namein (Modules) dolocalRequest=HttpService:RequestAsync({
Url=`{Base}{Name}.luau`;
Method="GET";
});
localSuccess=Request.SuccessandRequest.StatusCode==200ifnotSuccessthenerror(`Failed to install XMLParser module: {Name}`) endif (Name=="init") thenXMLModule.Source=Request.BodyelselocalChild=Instance.new("ModuleScript");
Child.Name=Name;
Child.Source=Request.Body;
Child.Parent=XMLModule;
endendHttpService.HttpEnabled=LastValueXMLModule.Parent=ReplicatedStorage;
print("Successfully installed XMLParser module. At:", XMLModule);Wally
xmlparser="perthys/xmlparser@2.0.0"Example
localReplicatedStorage=game:GetService("ReplicatedStorage");
localXML=require(ReplicatedStorage:WaitForChild("XML"));
localTagStart=`<b>`;
localTagEnd=`</b>`;
localTagStartArg=`<font size="50">`localTagEndArg=`</font>`localSingle=`<image ImageID="rbxassetid://15102015050" OtherArgument="test"/>`localProlog=`<?xml version="1.0" encoding="UTF-8"?>`localComment=`<!-- text -->`localText="hello";
Text=`{TagStart}{Text}{TagEnd}`Text=`{TagStartArg}{Text}{TagEndArg}`Text=`{Single}{Text}`Text=`{Prolog}{Text}`Text=`{Comment}{Text}`print(Text)
localTree=XML:GenerateTreeFromXML(Text); print(Tree)
localNewText=XML:GenerateXMLFromTree(Tree); print(NewText==Text) -- trueExample Output
localReplicatedStorage=game:GetService("ReplicatedStorage");
localXML=require(ReplicatedStorage:WaitForChild("XML"));
localTagStart=`<b>`;
localTagEnd=`</b>`;
localTagStartArg=`<font size="50">`localTagEndArg=`</font>`localSingle=`<image ImageID="rbxassetid://15102015050" OtherArgument="test"/>`localProlog=`<?xml version="1.0" encoding="UTF-8"?>`localComment=`<!-- text -->`localText="hello";
Text=`{TagStart}{Text}{TagEnd}`Text=`{TagStartArg}{Text}{TagEndArg}`Text=`{Single}{Text}`Text=`{Prolog}{Text}`Text=`{Comment}{Text}`localTree= {
Type="ROOT",
Attributes= {},
Children= {
{
Type="COMMENT",
Text=" text ",
Attributes= {},
Children= {}
},
{
Type="PROLOG",
TagType="xml",
Attributes= {
{ Name="version", Value="1.0" },
{ Name="encoding", Value="UTF-8" }
},
Children= {}
},
{
Type="SINGLE",
TagType="image",
Attributes= {
{ Name="ImageID", Value="rbxassetid://15102015050" },
{ Name="OtherArgument", Value="test" }
},
Children= {}
},
{
Type="TAG",
TagType="font",
Attributes= {
{ Name="size", Value="50" }
},
Children= {
{
Type="TAG",
TagType="b",
Attributes= {},
Children= {
{
Type="STRING",
Text="hello",
Attributes= {},
Children= {}
}
}
}
}
}
}
}
print(Text==XML:GenerateXMLFromTree(Tree)) -- trueXML:GenerateTreeFromXML(XML: string)-> XML_AST: {}XML:GenerateXMLFromTree(XML_AST: {})-> XML: stringXML.Escape(Text: string)-> string - escapes & < > " ' into entities XML.Unescape(Text: string)-> string - decodes & < > " ' A A
Node methodsNode:GetAttribute(Name: string)-> string? - entity-decoded attribute value Node:GetText()-> string? - entity-decoded text of a STRING/COMMENT node (Node.Text keeps the raw source) Node:GetChildren() / Node:AddChild(Node) / Node:RemoveChild(Node)
- Perth |
Perthys#0