XML Basics
This page is mostly theory and will focus on concepts and basic information without putting it into practice.
The objective of this article is to understand how XML and its components work altogether. You will be able to read basic XML syntax.
Nodes and elements
Time to learn how to read XML!
XML works with something called "nodes", which are several elements in an XML document. These nodes can have an opening tag and a closing tag, while the value of the element is inside those tags.
In this case, we have an element called range, with a value of 30.
<range>
is the opening tag, while</range>
is the closing tag.
Nodes have a hierarchical relationship:
book
,title
,Guide to Modding
,author
, andRandomPotato
are all nodes.- While
Guide to Modding
andRandomPotato
are values, they are also considered nodes. - Due to XML's hierarchy, book is a parent node, while title and author are child nodes.
- Child nodes are the data and information that the parent nodes have.
In everyday language, if we say "I'm going to add an element/tag", it is inferred that the opening tag, closing tag, and value will be added (like <element>value</element>
).
Empty elements also exist, they can be written as: <element></element>
, or just <element />
.
Prolog
<?xml version="1.0" encoding="UTF-8"?>
Optional but recommended. The XML Prolog is an specific line that tells the code (in this case, the game) what encoding to use. It is used to prevent errors with special characters. It must go in the first line of the XML document.
- For further reading: https://www.ibm.com/docs/en/integration-bus/10.1?topic=declaration-xml-encoding
Your XML document won't break if it misses this line, but it is still a best practice to use it.
Attributes
Attributes are additional information defined in a different way than regular elements. In this example, the attribute genre has the value fantasy. There are considerably fewer attributes in an element than child nodes. RimWorld uses specific attributes for specific purposes (we will see that later).
- Attribute's values must be quoted (
" "
). Example:<RandomTag SomeAttribute="SomeValue"></RandomTag>
Comments
Commenting is another powerful tool for XML if you want to write down "notes". Comments don't get read or executed at all, they exist only for other readers. People often put notes as comments for themselves OR to explain something to others.
Comments has the following syntaxis:
Example:
In this example, nothing written on green will be read by the game. It is completely safe and doesn't impact anything in the slightest.
Rules
- All elements must have a closing tag, if you omit a
</element>
then all your document is wrong and will fail:
- The whole document must be enclosed inside a root element that contains everything:
- Elements are case-sensitive. You should always try to be as precise and exact as possible. If you write
<steel>
instead of<Steel>
, RimWorld may not be able to recognize what that is. - You shouldn't have spaces inside the tags, such as
<my cool Tag></my cool Tag>
Further reading
W3School have simple and straightforward guides on several coding topics. Check https://www.w3schools.com/xml/xml_whatis.asp to learn more about XML.