- Feel free to join if you have any bugs or questions!
UdonXML is an XML parser written in Udon for VRChat. The purpose of this project is for it to be used as an API in other bigger projects. This work is something the average VRChatter never will notice, but something the author hopes might be beneficial and make the life easier for world/game creators in VRChat.
An example use of this library is allowing the player to paste the entire contents of an XML file (e.g. game save file) into an Input field in VRC, and allowing the world to then parse the submitted data.dates. For this purpose a Zip parser library has also been implemented, which you can find here, called UdonZip.
- Unity 2018.4.20f1
- VRCSDK3
- Latest UdonSharp
- Go to Releases and download the latest release. The file should end with
unitypackage
. If you can't find it, ask for help on Discord. - Import the package into Unity with Assets > Import Package > Custom Package. This will import the files to
Resources > Foorack > UdonXML
. - Create an Empty GameObject in your Scene, name it
UdonXML
and assign it the UdonXML UdonBehaviour.
- Declare a
public UdonXML udonXml;
variable in your program. - Assign it the value of the UdonXML GameObject in your scene.
- Parse your XML data with LoadXml
udonXml.LoadXml(inputData);
. It will return an object. - The object returned represents the root of the xml tree, use it when executing other API functions such as
GetNodeName
orHasChildNodes
.
using UnityEngine;
using UdonSharp;
public class UdonXMLTest : UdonSharpBehaviour
{
public UdonXML udonXml;
private string EXAMPLE_DATA = @"<?xml version=""1.0"" encoding=""utf-8""?>
<books xmlns=""http://www.contoso.com/books"">
<book genre=""novel"" ISBN=""1-861001-57-8"" publicationdate=""1823-01-28"">
<title>Pride And Prejudice</title>
<price>24.95</price>
</book>
<book genre=""novel"" ISBN=""1-861002-30-1"" publicationdate=""1985-01-01"">
<title>The Handmaid's Tale</title>
<price>29.95</price>
</book>
<book genre=""novel"" ISBN=""1-861001-45-3"" publicationdate=""1811-01-01"">
<title>Sense and Sensibility</title>
<price>19.95</price>
</book>
</books>";
public void Start()
{
// Parse and store the root node
var root = udonXml.LoadXml(EXAMPLE_DATA);
// Fetch the first <books> node by index
var books = udonXml.GetChildNode(root, 1); // Index 0 will be <?xml> tag
// Loop over all children in the <books> node
for (var bookNum = 0; bookNum != udonXml.GetChildNodesCount(books); bookNum++)
{
var book = udonXml.GetChildNode(books, bookNum);
// Fetch <title> and <price> nodes by tag name.
var title = udonXml.GetChildNodeByName(book, "title");
var price = udonXml.GetChildNodeByName(book, "price");
Debug.Log("title: " + udonXml.GetNodeValue(title) + " price: " + udonXml.GetNodeValue(price));
}
}
}
Loads an XML structure into memory by parsing the provided input.
Returns null in case of parse failure.
Saves the stored XML structure in memory to an XML document.
Uses default indent of 4 spaces. Use SaveXmlWithIdent
to override.
Saves the stored XML structure in memory to an XML document with given indentation.
Returns true if the node has child nodes.
Returns the number of children the current node has.
Returns the child node by the given index.
Returns the child node by the given name.
If multiple nodes exists with the same type-name then the first one will be returned.
Returns the type-name of the node.
Returns the value of the node.
Returns whether the node has a given attribute or not.
Returns the value of the attribute by given name.
Creates a new child node under the current given node.
Returns the newly created node.
Removes a child node from the current node by given index.
Returns the deleted node.
Removes a child node from the current node by given name. If multiple nodes exist with the same name then only the first one is deleted.
Returns the deleted node, or null if not found.
Sets the name of the node.
Sets the value of the node.
Sets the value of an attribute on given node.
Returns false if the attribute already existed, returns true if attribute was created.
Removes an attribute by name in the given node.
Returns true if attribute was deleted, false if it did not exist.
Parsing from in-memory back to an XML document.Allowing setting values and creating nodes.- Improve error detection / make it stricter. Right now it allows closing tag to have a different name from opening tag, and only goes by level depth.
Add support for DOCTYPE and storing the ?xml declaration.
Functionality of validating an XML document's compliance against an XML Schema is very far-reach and out-of-scope for the foreseeable future.