Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Understanding XML

From Farming Simulator Modding Wiki
Revision as of 17:45, 27 December 2024 by XPModder (talk | contribs) (Created page with "XML is a way of structuring information in a text file. The file ending .xml indicates that a files content is structured using XML, but a file can still be structured in XML even if it has a different file ending. Understanding the concept of XML, how information is structured and what individual parts are named is important for FS modding as the two most common files used in mods - xml and i3d files - are both structured in XML. Additionally many pages and tutorial...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

XML is a way of structuring information in a text file.

The file ending .xml indicates that a files content is structured using XML, but a file can still be structured in XML even if it has a different file ending.

Understanding the concept of XML, how information is structured and what individual parts are named is important for FS modding as the two most common files used in mods - xml and i3d files - are both structured in XML. Additionally many pages and tutorials on this wiki regularly use names and phrases referring to specific components in an xml, so understanding what these terms mean can be important to understand the tutorials correctly.

If you are not familiar with XML and the terminology used when talking about it, please read this page carefully to be able to better understand XML and the terms used throughout this wiki.


XML structures information in a hierarchy with each element being able to contain information directly and contain additional elements inside it simultaneously.

Looking at any element A, if it contains other elements (B, C, D, etc) these are referred to as the children of the element A and element A is referred to as the parent of elements B, C, D, etc.

Note that in this structure, every element can have an unlimited number of children, but can only ever have ONE parent. Additionally a element can have no children at all, but every element, with exception of the root element and the xml element, has exactly one parent.

Naming convention

Tags and attributes in XML generally follow the same naming convention:

Names can be any arbitrary string as long as it consists only of letters a-z. Both lowercase and uppercase letters are allowed. Naming convention is to use only lowercase letters unless the name consists of multiple words, in which case the first letter of every word except for the first word is capitalized. Some examples for naming convention:

  • something -> something
  • something big -> somethingBig
  • fruit type -> fruitType
  • more then two words -> moreThenTwoWords

Tags

The elements of a XML are called Tags.

There are three types of normal tags: Opening tags, Closing tags and Self-closing tags.

Additionally to those, some special tags like the xml version tag and the comment tag exist. These have their own subsections below.

Names of tags follow the naming convention explained above.

Opening tags without attributes merely consist of the tag name enclosed in angled brackets like this:

<tagName>

If a tag has attributes, those are in the opening tag behind the tag name and separated by spaces. See Attributes section below for more info on them. Closing tags always only consist of the tag name, which has to be equal to that of the matching opening tag enclosed in angle brackets with a slash "/" directly after the opening angle bracket, which indicates that this is a closing tag. It looks like this:

</tagName>

Closing tags cannot contain attributes.

Self-closing tags are a special case and can be used instead of a pair of opening and closing tags when the tag has no children.

Self-closing tags can have attributes, but cannot have children.

A self-closing tag begins with a opening angle bracket "<" followed directly by the tag name and ends with a slash followed directly by a closing angle bracket "/>". Here is a simple example without attributes:

<tagName/>

XML version tag

The XML version tag is a special kind of tag which is placed outside all other tags in the first line of a xml file. It looks like this:

<?xml version="1.0" encoding="utf-8"?>

It specifies the file as being a xml file and specifies the xml version and the encoding used for the contents of the file.

Note that the xml version tag has to be in the very first line of the file to be parsed correctly.

Comment tags

When creating large xml files, one may wish to comment something to make it easier to read for any person who may end up looking at the file. Comments are only meant for other people to read and are completely ignored by any program reading the file.

XML does not distinguish between single-line and multi-line comments.

A comment begins with the string "". Everything between these two will be seen as a comment regardless of how many or how few characters or lines this includes.

Here are some examples of comments in xml:


Attributes

Cookies help us deliver our services. By using our services, you agree to our use of cookies.