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

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 and digits 0-9. 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
  • some tag 1 -> someTag1

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 "<!--" and ends with "-->". 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:

<!-- This is a small single line comment -->

<!-- This is also all
a comment
regarless of
how many
lines it is on -->

<!-- Of course this
<and even="all" of="this">
<andThis/>
</and>

this is part of the comment -->

Attributes

Where the tags mostly store the hierarchy, attributes are one way to store information in an xml file.

Every xml tag can have any number of attributes.

Attributes consist of the attribute name and value and always follow one of two formats. Either a name and value pair name=value or only a name name . Attribute names also follow the naming convention explained above.

It is important to note that spaces are NOT allowed between the attribute name and the = or between the = and the value.

Multiple attributes are separated with spaces.

A pair of opening and closing tag with attributes can therefore look like this: <someTag attribute="my String" length=24 something ></someTag>

Similarly a self-closing tag with attributes can look like this: <someTag attribute="my String" length=24 something />

The space between the last attribute and the closing bracket or slash with closing bracket is optional, but often used for easier readability.

Tag contents

In addition to attributes information can also be stored in the contents of a tag. This is only possible on pairs of opening and closing tags. To store information in the contents of a tag, the information is simply put between the opening and the closing tag like this:

<someTag>This is tag content. This can be as short or as long as you want.</someTag>
<someTag>It is even possible to have content and other tags <mixed in="with each other"/></someTag>

Root tag/Root element

The root tag or root element of a xml file is the first tag in a xml file, excluding the XML version tag. Every xml file has exactly ONE root tag.

The root tag is the only tag in a xml file that has no parent. Otherwise it acts like any normal tag. The root tag can have attributes and content and can even be a self-closing tag.

Examples

Here are some examples of xml files used in FS.

Farmlands xml example
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<farmlands>
    <farmland id="1" farmId="1"/>
    <farmland id="2" farmId="1"/>
    <farmland id="3" farmId="1"/>
    <farmland id="4" farmId="1"/>
    <farmland id="5" farmId="1"/>
    <farmland id="6" farmId="1"/>
    <farmland id="7" farmId="1"/>
    <farmland id="8" farmId="1"/>
    <farmland id="9" farmId="1"/>
    <farmland id="10" farmId="1"/>
    <farmland id="11" farmId="1"/>
    <farmland id="12" farmId="1"/>
    <farmland id="13" farmId="1"/>
    <farmland id="14" farmId="1"/>
    <farmland id="15" farmId="1"/>
    <farmland id="16" farmId="1"/>
    <farmland id="17" farmId="1"/>
    <farmland id="18" farmId="1"/>
    <farmland id="19" farmId="1"/>
    <farmland id="20" farmId="1"/>
    <farmland id="21" farmId="1"/>
    <farmland id="22" farmId="1"/>
    <farmland id="23" farmId="1"/>
    <farmland id="24" farmId="1"/>
    <farmland id="25" farmId="1"/>
    <farmland id="26" farmId="1"/>
    <farmland id="27" farmId="1"/>
    <farmland id="28" farmId="1"/>
    <farmland id="29" farmId="1"/>
    <farmland id="30" farmId="1"/>
    <farmland id="31" farmId="1"/>
    <farmland id="32" farmId="1"/>
    <farmland id="33" farmId="1"/>
    <farmland id="34" farmId="1"/>
    <farmland id="35" farmId="1"/>
    <farmland id="36" farmId="1"/>
    <farmland id="37" farmId="1"/>
    <farmland id="38" farmId="1"/>
    <farmland id="39" farmId="1"/>
    <farmland id="40" farmId="1"/>
    <farmland id="41" farmId="1"/>
    <farmland id="42" farmId="1"/>
    <farmland id="43" farmId="1"/>
    <farmland id="44" farmId="1"/>
    <farmland id="45" farmId="1"/>
    <farmland id="46" farmId="1"/>
    <farmland id="47" farmId="1"/>
    <farmland id="48" farmId="1"/>
    <farmland id="49" farmId="1"/>
    <farmland id="50" farmId="1"/>
    <farmland id="51" farmId="1"/>
    <farmland id="52" farmId="1"/>
    <farmland id="53" farmId="1"/>
    <farmland id="54" farmId="1"/>
    <farmland id="55" farmId="1"/>
    <farmland id="56" farmId="1"/>
    <farmland id="57" farmId="1"/>
    <farmland id="58" farmId="1"/>
    <farmland id="59" farmId="1"/>
    <farmland id="60" farmId="1"/>
    <farmland id="61" farmId="1"/>
    <farmland id="62" farmId="1"/>
    <farmland id="63" farmId="1"/>
    <farmland id="64" farmId="1"/>
    <farmland id="65" farmId="1"/>
    <farmland id="66" farmId="1"/>
    <farmland id="67" farmId="1"/>
    <farmland id="68" farmId="1"/>
    <farmland id="69" farmId="1"/>
    <farmland id="70" farmId="1"/>
    <farmland id="71" farmId="1"/>
    <farmland id="72" farmId="1"/>
    <farmland id="73" farmId="1"/>
    <farmland id="74" farmId="1"/>
    <farmland id="75" farmId="1"/>
    <farmland id="76" farmId="1"/>
    <farmland id="77" farmId="1"/>
    <farmland id="78" farmId="1"/>
    <farmland id="79" farmId="1"/>
    <farmland id="80" farmId="1"/>
    <farmland id="81" farmId="1"/>
    <farmland id="82" farmId="1"/>
    <farmland id="83" farmId="1"/>
    <farmland id="84" farmId="1"/>
    <farmland id="85" farmId="1"/>
    <farmland id="86" farmId="1"/>
    <farmland id="87" farmId="1"/>
    <farmland id="88" farmId="1"/>
    <farmland id="89" farmId="1"/>
    <farmland id="90" farmId="1"/>
    <farmland id="91" farmId="1"/>
    <farmland id="255" farmId="1"/>
</farmlands>


The above is the farmlands.xml from a FS savegame. This is an example of a relatively simple xml file. It begins with the xml version tag and then follows the root tag, which is the <farmlands> tag here. The root tag then contains a number of children which are all named farmland and each have two attributes: id and farmId with their respective values.


maps_fruitTypes xml example
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../shared/xml/schema/fruitTypes.xsd">
    <fruitTypes>
        <!--fruitType filename="$data/foliage/wheat/wheat.xml"/>
        <fruitType filename="$data/foliage/barley/barley.xml"/>
        <fruitType filename="$data/foliage/canola/canola.xml"/>
        <fruitType filename="$data/foliage/oat/oat.xml"/>
        <fruitType filename="$data/foliage/maize/maize.xml"/>
        <fruitType filename="$data/foliage/sunflower/sunflower.xml"/>
        <fruitType filename="$data/foliage/soybean/soybean.xml"/>
        <fruitType filename="$data/foliage/potato/potato.xml"/>
        <fruitType filename="$data/foliage/rice/rice.xml"/>
        <fruitType filename="$data/foliage/riceLongGrain/riceLongGrain.xml"/>
        <fruitType filename="$data/foliage/sugarbeet/sugarbeet.xml"/>
        <fruitType filename="$data/foliage/sugarcane/sugarcane.xml"/>
        <fruitType filename="$data/foliage/cotton/cotton.xml"/>
        <fruitType filename="$data/foliage/sorghum/sorghum.xml"/>
        <fruitType filename="$data/foliage/grape/grape.xml"/>
        <fruitType filename="$data/foliage/olive/olive.xml"/>
        <fruitType filename="$data/foliage/poplar/poplar.xml"/>
        <fruitType filename="$data/foliage/beetRoot/beetRoot.xml"/>
        <fruitType filename="$data/foliage/carrot/carrot.xml"/>
        <fruitType filename="$data/foliage/parsnip/parsnip.xml"/>
        <fruitType filename="$data/foliage/greenBean/greenBean.xml"/>
        <fruitType filename="$data/foliage/pea/pea.xml"/>
        <fruitType filename="$data/foliage/spinach/spinach.xml"/>
        <fruitType filename="$data/foliage/grass/grass.xml"/>
        <fruitType filename="$data/foliage/oilseedRadish/oilseedRadish.xml"/-->

		<!-- New Fruits -->
        <fruitType filename="map/foliage/alfalfa/alfalfa.xml"/>
        <fruitType filename="map/foliage/clover/clover.xml"/>
        <fruitType filename="map/foliage/horseGrass/horseGrass.xml"/>
        <fruitType filename="map/foliage/lavender/lavender.xml"/>
        <fruitType filename="map/foliage/millet/millet.xml"/>
        <fruitType filename="map/foliage/onion/onion.xml"/>
        <fruitType filename="map/foliage/poppy/poppy.xml"/>
        <fruitType filename="map/foliage/raspberry/raspberry.xml"/>
        <fruitType filename="map/foliage/rye/rye.xml"/>
        <fruitType filename="map/foliage/sesame/sesame.xml"/>
        <fruitType filename="map/foliage/spelt/spelt.xml"/>
        <fruitType filename="map/foliage/triticale/triticale.xml"/>
    </fruitTypes>

    <fruitTypeCategories>
        <fruitTypeCategory name="GRAINHEADER" >SESAME WHEAT BARLEY OAT CANOLA SOYBEAN SORGHUM RYE SPELT TRITICALE MILLET POPPY RICELONGGRAIN</fruitTypeCategory>
        <fruitTypeCategory name="MAIZEHEADER" >MAIZE SUNFLOWER</fruitTypeCategory>
        <fruitTypeCategory name="MAIZECUTTER" >MAIZE WHEAT BARLEY OAT CANOLA SOYBEAN SORGHUM SUNFLOWER RYE SPELT TRITICALE MILLET LAVENDER</fruitTypeCategory>
        <fruitTypeCategory name="DIRECTCUTTER" >SESAME GRASS WHEAT BARLEY OAT CANOLA SOYBEAN SORGHUM SUNFLOWER RYE SPELT TRITICALE MILLET ALFALFA CLOVER HORSEGRASS LAVENDER</fruitTypeCategory>
        <fruitTypeCategory name="PICKUP" >GRASS ALFALFA CLOVER HORSEGRASS ONION</fruitTypeCategory>
        <fruitTypeCategory name="SOWINGMACHINE" >SESAME WHEAT BARLEY OAT CANOLA SOYBEAN SORGHUM OILSEEDRADISH GRASS RYE SPELT TRITICALE MILLET ALFALFA POPPY CLOVER HORSEGRASS RICELONGGRAIN PEA SPINACH</fruitTypeCategory>
        <fruitTypeCategory name="SUGARCANE_PLANTER" >SUGARCANE</fruitTypeCategory>
        <fruitTypeCategory name="PLANTER" >MAIZE SUNFLOWER SOYBEAN SUGARBEET COTTON CARROT ONION PARSNIP BEETROOT LAVENDER GREENBEAN</fruitTypeCategory>
        <fruitTypeCategory name="WEEDER" >OILSEEDRADISH GRASS ALFALFA CLOVER HORSEGRASS</fruitTypeCategory>
        <fruitTypeCategory name="TOPLIFTINGHARVESTER">CARROT BEETROOT PARSNIP</fruitTypeCategory>
        <fruitTypeCategory name="PLANTER_SMALL">CARROT BEETROOT PARSNIP SPINACH</fruitTypeCategory>
    </fruitTypeCategories>

    <fruitTypeConverters>
        <fruitTypeConverter name="FORAGEHARVESTER">
            <converter from="MAIZE"     to="CHAFF"         factor="7.8" />
            <converter from="GRASS"     to="GRASS_WINDROW" factor="1.0" windrowFactor="1.0" />
            <converter from="WHEAT"     to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion -->
            <converter from="BARLEY"    to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion -->
            <converter from="OAT"       to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion -->
            <converter from="CANOLA"    to="CHAFF"         factor="4.0" />
            <converter from="SUNFLOWER" to="CHAFF"         factor="6.0" />
            <converter from="SOYBEAN"   to="CHAFF"         factor="4.0" />
            <converter from="SORGHUM"   to="CHAFF"         factor="4.0" />
            <converter from="POPLAR"    to="WOODCHIPS"     factor="3.0" />
            <converter from="RYE"       to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion -->
	        <converter from="SPELT"     to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion -->
	        <converter from="TRITICALE" to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion -->
            <converter from="MILLET"    to="CHAFF"         factor="4.0" />
            <converter from="LAVENDER"  to="LAVENDER"      factor="1.0" />
            <converter from="SESAME"    to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion -->
        </fruitTypeConverter>
        <fruitTypeConverter name="MOWER">
            <converter from="GRASS"         to="GRASS_WINDROW" factor="1.0" windrowFactor="1.0" />
            <converter from="MEADOW"        to="GRASS_WINDROW" factor="1.0" windrowFactor="1.0" />
            <converter from="WHEAT"         to="STRAW"         factor="1.0" windrowFactor="1.0" />
            <converter from="BARLEY"        to="STRAW"         factor="1.0" windrowFactor="1.0" />
            <converter from="OAT"           to="STRAW"         factor="1.0" windrowFactor="1.0" />
            <converter from="ALFALFA"       to="ALFALFA_WINDROW" factor="1.0" windrowFactor="1.0" />
            <converter from="RYE"           to="STRAW"         factor="1.0" windrowFactor="1.0" />
	        <converter from="SPELT"         to="STRAW"         factor="1.0" windrowFactor="1.0" />
	        <converter from="TRITICALE"     to="STRAW"         factor="1.0" windrowFactor="1.0" />
            <converter from="CLOVER"        to="CLOVER_WINDROW" factor="1.0" windrowFactor="1.0" />
            <converter from="HORSEGRASS"    to="HORSEGRASS_WINDROW" factor="1.0" windrowFactor="1.0" />
            <converter from="SESAME"        to="STRAW"         factor="1.0" windrowFactor="1.0" />
        </fruitTypeConverter>
    </fruitTypeConverters>
</map>


The above is an example of a maps_fruitTypes.xml and is a slightly more complex xml file. We can see that the root tag here is named map and has several child tags all of which also have their own children. We can see comments in multiple locations throughout the file. Many of the tags in this xml have attributes, but the fruitTypeCategory tags also have information in their tag contents.


Note that both examples have used indentation to make the file easily readable by a person, but in theory no indentation or line breaks between tags are necessary. The following would also be valid xml and a program would read it just as easily as the examples above.

<fruitTypeConverters><fruitTypeConverter name="FORAGEHARVESTER"><converter from="MAIZE"     to="CHAFF"         factor="7.8" /><converter from="GRASS"     to="GRASS_WINDROW" factor="1.0" windrowFactor="1.0" /><converter from="WHEAT"     to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion --><converter from="BARLEY"    to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion --><converter from="OAT"       to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion --><converter from="CANOLA"    to="CHAFF"         factor="4.0" /><converter from="SUNFLOWER" to="CHAFF"         factor="6.0" /><converter from="SOYBEAN"   to="CHAFF"         factor="4.0" /><converter from="SORGHUM"   to="CHAFF"         factor="4.0" /><converter from="POPLAR"    to="WOODCHIPS"     factor="3.0" /><converter from="RYE"       to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion --><converter from="SPELT"     to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion --><converter from="TRITICALE" to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion --><converter from="MILLET"    to="CHAFF"         factor="4.0" /><converter from="LAVENDER"  to="LAVENDER"      factor="1.0" /><converter from="SESAME"    to="CHAFF"         factor="4.0" windrowFactor="1.0" /> <!-- straw 1:1 conversion --></fruitTypeConverter><fruitTypeConverter name="MOWER"><converter from="GRASS"         to="GRASS_WINDROW" factor="1.0" windrowFactor="1.0" /><converter from="MEADOW"        to="GRASS_WINDROW" factor="1.0" windrowFactor="1.0" /><converter from="WHEAT"         to="STRAW"         factor="1.0" windrowFactor="1.0" /><converter from="BARLEY"        to="STRAW"         factor="1.0" windrowFactor="1.0" /><converter from="OAT"           to="STRAW"         factor="1.0" windrowFactor="1.0" /><converter from="ALFALFA"       to="ALFALFA_WINDROW" factor="1.0" windrowFactor="1.0" /><converter from="RYE"           to="STRAW"         factor="1.0" windrowFactor="1.0" /><converter from="SPELT"         to="STRAW"         factor="1.0" windrowFactor="1.0" /><converter from="TRITICALE"     to="STRAW"         factor="1.0" windrowFactor="1.0" /><converter from="CLOVER"        to="CLOVER_WINDROW" factor="1.0" windrowFactor="1.0" /><converter from="HORSEGRASS"    to="HORSEGRASS_WINDROW" factor="1.0" windrowFactor="1.0" /><converter from="SESAME"        to="STRAW"         factor="1.0" windrowFactor="1.0" /></fruitTypeConverter></fruitTypeConverters>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.