Guide: Add an xml tag - We-the-People-civ4col-mod/Mod GitHub Wiki
How to add a tag to an xml file
This example shows how to add a bool. First the class header for the info class in question:
public:
inline bool isIgnoresBoycott() const {return m_bIgnoresBoycott;}
private:
bool m_bIgnoresBoycott;
Next it needs to be initialized in the class constructor, in this case:
m_bIgnoresBoycott(false),
Last part of the C++ code is the read(pXML) function, which would look like this:
bool CvBuildingInfo::read(CvXMLLoadUtility* pXML)
...
pXML->GetChildXmlValByName(&m_bIgnoresBoycott, "bIgnoresBoycott");
In the schema file:
<ElementType name="bIgnoresBoycott" content="textOnly" dt:type="boolean"/>
and in the class in question
<element type="bIgnoresBoycott" minOccurs="0"/>
Explanation
Declaring an ElementType in the schema tells what to expect when the tag is encountered. In this case it's a text element and it's intended to be read as a bool value. In the class it lists the elements. They have to be entered in the order written here. minOccurs and maxOccurs sets the interval for how many times the tag should be repeated, both defaults to 1. In this case min is 0, meaning it's ok to skip it entirely when filling out the xml file. This is the preferred approach if you can accept a default value.
In xml, pXML->GetChildXmlValByName() is used to read the variable. It's an overloaded function and there are plenty of different ways to call it, including list readings. In this case the argument is a bool reference, meaning it will read a bool tag. The next argument is what the tag is named and the last argument is the default value, which for bools defaults to false (hence not written).
This means minOccur=0 allows the tag to be skipped and pXML->GetChildXmlValByName() sets the bool to false if it's not in xml.
The approach for ints is the same and it defaults to 0.
The xml editor assumes the default values to be 0/false
TODO: write about advanced xml topics like lists and other ways to nest xml elements.
The code examples are from Added bIgnoresBoycott tag to CvBuildingInfo.