Menu

High level functions are perhaps required

Shaul
2013-06-11
2013-06-11
  • Shaul

    Shaul - 2013-06-11

    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-->
        <SmartPropoPlus Version="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:

           TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
    

    That is a create a leaf element AND the path leading to it in one line of code. Something like this:

           TiXmlElement* child2 = docHandle.Child( "Document" ).Child( "Element" ).Child( "Child").CreateElement();
    

    Please comment on this. Thank you.

     
  • Jack charles

    Jack charles - 2023-04-11
    Post awaiting moderation.

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.