I begun using TinyXml a few days ago as I move my project from Registry to XML. The learning curve was very steep, perhaps thanks to the neat class hierarchy of TinyXml. The documentation, may I add, was OK, not more.
Although TinyXml gives you efficient tool to build ANY xml file - it lack, so I feel, the High level functions that might make your life easier when creating a simple xml file.
Take this following simple structure:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><!--This file holds the configuration of SmartPropoPlus - Tampering with it will not be wise--><SmartPropoPlusVersion="4"SubVersion="1"><Targets><vJoy_Device><Id>1</Id><Device_Map><Axes><SL1>1</SL1><SL0>2</SL0><RZ>3</RZ><RY>4</RY><RX>5</RX><Z>10</Z><Y>9</Y><X>8</X></Axes></Device_Map></vJoy_Device></Targets></SmartPropoPlus>
Note how much work you should do to write/change every leaf (X, Y ... SL1) even when having a handle to element <Axes>
I wrote a helper function that others might find useful:
~~~~~~
// Create/Replace a UNIQUE leaf element with its text
// Parameters:
// Parent: Handle of the parent element
// LeafName: The name of the leaf to create (if does not exist)
// LeafText: The text to insert in the leaf element
// Replace: If the leaf element exist - should the text be replaced
// Returns: Handle to leaf element if successful - NULL otherwize
TiXmlHandle UniqueTextLeaf(TiXmlHandle Parent, std::string &LeafName, std::string &LeafText, bool Replace)
{
// Does the leaf exist and unique
TiXmlElement * Leaf[2];
Leaf[0] = Parent.ChildElement(LeafName, 0).ToElement();
Leaf[1] = Parent.ChildElement(LeafName, 1).ToElement();
if (Leaf[0] && Leaf[1]) // Leaf not unique
return 0;
if (!Replace && Leaf[0]) // Leaf already exists and not to be replaced
return 0;
if (!Leaf[0]) // Leaf does not exist - Create it
{
Leaf[0] = new TiXmlElement(LeafName);
Parent.ToElement()->LinkEndChild(Leaf[0]);
}
// Replace
Leaf[0]->Clear();
Leaf[0]->LinkEndChild(new TiXmlText(LeafText));
TiXmlHandle LeafHandle(Leaf[0]);
return LeafHandle;
}
~~~~~
Another suggestion would be to define a function that does the reverse of the following:
I begun using TinyXml a few days ago as I move my project from Registry to XML. The learning curve was very steep, perhaps thanks to the neat class hierarchy of TinyXml. The documentation, may I add, was OK, not more.
Although TinyXml gives you efficient tool to build ANY xml file - it lack, so I feel, the High level functions that might make your life easier when creating a simple xml file.
Take this following simple structure:
Note how much work you should do to write/change every leaf (X, Y ... SL1) even when having a handle to element <Axes>
I wrote a helper function that others might find useful:
~~~~~~
// Create/Replace a UNIQUE leaf element with its text
// Parameters:
// Parent: Handle of the parent element
// LeafName: The name of the leaf to create (if does not exist)
// LeafText: The text to insert in the leaf element
// Replace: If the leaf element exist - should the text be replaced
// Returns: Handle to leaf element if successful - NULL otherwize
TiXmlHandle UniqueTextLeaf(TiXmlHandle Parent, std::string &LeafName, std::string &LeafText, bool Replace)
{
// Does the leaf exist and unique
TiXmlElement * Leaf[2];
Leaf[0] = Parent.ChildElement(LeafName, 0).ToElement();
Leaf[1] = Parent.ChildElement(LeafName, 1).ToElement();
if (Leaf[0] && Leaf[1]) // Leaf not unique
return 0;
}
~~~~~
Another suggestion would be to define a function that does the reverse of the following:
That is a create a leaf element AND the path leading to it in one line of code. Something like this:
Please comment on this. Thank you.