user1364743
user1364743

Reputation: 5661

Find a specific node in a XML document with TinyXML

I want to parse some data from an xml file with TinyXML.

Here's my text.xml file content:

<?xml version="1.0" encoding="iso-8859-1"?>
<toto>
  <tutu>
    <tata>
      <user name="toto" pass="13" indice="1"/>
      <user name="tata" pass="142" indice="2"/>
      <user name="titi" pass="azerty" indice="1"/>
    </tata>
  </tutu>
</toto>

I want to access to the first element 'user'. The way to do this is the following :

TiXmlDocument   doc("test.xml");

    if (doc.LoadFile())
    {
        TiXmlNode *elem = doc.FirstChildElement()->FirstChildElement()->FirstChildElement()->FirstChildElement();
std::cout << elem->Value() << std::endl;
}

In output : user.

But the code is pretty ugly and not generic. I tried the code below to simulate the same behaviour than the code above but it doesn't work and an error occured.

TiXmlElement *getElementByName(TiXmlDocument &doc, std::string const &elemt_value) 
{
    TiXmlElement *elem = doc.FirstChildElement(); //Tree root

    while (elem)
    {
        if (!std::string(elem->Value()).compare(elemt_value))
            return (elem);
        elem = elem->NextSiblingElement();
    }
    return (NULL);
}

Maybe I missed a special function in the library which can do this work (a getElementByName function). I just want to get a pointer to the element where the value is the one I'm looking for. Does anyone can help me? Thanks in advance for your help.

Upvotes: 2

Views: 11388

Answers (5)

Dmitry
Dmitry

Reputation: 126

Actually, you want to find the first "toto/tutu/tata/user". Because imagine the document

<toto>
    <tutu>
        <user />
    </tutu>
    <user />
</toto>

What "user" do you regard the first, "toto/tutu/user" or "toto/user"? In your example, using TiXmlHandle could make things easier. TiXmlHandle::Child(), TiXmlHandle::FirstChild(), TiXmlHandle::ChildElement() check the argument for NULL.

TiXmlDocument doc("test.xml");

if (doc.LoadFile()) {
    TiXmlHandle h(&doc);

    TiXmlElement* elem = h
        .FirstChildElement()
        .FirstChildElement()
        .FirstChildElement()
        .Child("user", 0).ToElement(); //0 stands for "first occurrence"
    if (elem) {
        printf(" %s %s\n", elem->Value(), elem->Attribute("pass"));
    } else {
        printf("None");
    }
}

For even more convenience, consider using tinyxpath. There you can find your element by the string "/toto/tutu/tata/user". Or simply "*/user" for the first "user" met.

Upvotes: 0

Arpit Maiya
Arpit Maiya

Reputation: 177

XMLElement *getElementByName(XMLDocument &ele, std::string const &elemt_value)
    {
    XMLElement *elem = ele.FirstChildElement(); //Tree root

    while (elem)
    {
        if (!std::string(elem->Value()).compare(elemt_value))
            return elem;
        if (elem->FirstChildElement())
        {
            elem = elem->FirstChildElement();
        }
        else if (elem->NextSiblingElement())
        {
            elem = elem->NextSiblingElement();
        }
        else
        {
            if (elem->Parent()->ToElement()->NextSiblingElement())
            {
                elem = elem->Parent()->ToElement()->NextSiblingElement();
            }
            else if (elem->Parent()->ToElement()->FirstChildElement()
                        &&  strcmp(elem->Name(), elem->Parent()->ToElement()->FirstChildElement()->Name()))
            {
                elem = elem->Parent()->ToElement()->FirstChildElement();
            }
            else {
                break;
            }
        }
    }
    return NULL;
}

// A small tweak in the above given solution

Upvotes: 1

JKallio
JKallio

Reputation: 903

You can also iterate through your XML elements one-by-one by using recursive function combined with lamda-function as a handler.

// 
// This function will iterate through your XML tree and call the 'parseElement' function for each found element.
//
void RecursiveXMLParse(TiXmlElement* element, std::function<void(TiXmlElement*)>& parseElement)
{
    if (element != nullptr)
    {
        parseElement(element);
        auto child = element->FirstChildElement();
        if (child != nullptr)
        {
            RecursiveXMLParse(child, parseElement);
        }

        for (auto sibling = element->NextSiblingElement(); sibling != nullptr; sibling = sibling->NextSiblingElement())
        {
            RecursiveXMLParse(sibling, parseElement);
        }
    }
}

Usage: Just pass the XML root element, and your data handler lambda function to the recursive Parser-function.

int main()
{
    //
    // Define your data handler labmda
    //
    std::function<void(TiXmlElement*)>parseElement = [&](TiXmlElement* e) -> void
    {
        if (std::string(elem->Value()).compare("user"))
        {
             // Parse your user data
        }
    };

    // Pass the root element along with the above defined lambda to the recursive function 
    RecursiveXMLParse(doc.RootElement(), parseElement);

    return 0;
}

Upvotes: 1

Waywatcher
Waywatcher

Reputation: 21

Adi's answer didn't work when i just copy pasted it into my code, but i modified it and now it works fine for me. since i made quite a lot of changes i thought i should post my final code here.

    void parseXML(tinyxml2::XMLDocument& xXmlDocument, std::string sSearchString, std::function<void(tinyxml2::XMLNode*)> fFoundSomeElement)
    {
            if ( xXmlDocument.ErrorID() != tinyxml2::XML_SUCCESS )
            {
                // XML file is not ok ... we throw some exception
                throw DataReceiverException( "XML file parsing failed" );
            } // if

            //ispired by http://stackoverflow.com/questions/11921463/find-a-specific-node-in-a-xml-document-with-tinyxml
            tinyxml2::XMLNode * xElem = xXmlDocument.FirstChild();
            while(xElem)
            {
                if (xElem->Value() && !std::string(xElem->Value()).compare(sSearchString))
                {
                    fFoundSomeElement(xElem);
                }

                /*
                *   We move through the XML tree following these rules (basically in-order tree walk):
                *   
                *   (1) if there is one or more child element(s) visit the first one
                *       else
                *   (2)     if there is one or more next sibling element(s) visit the first one
                *               else
                *   (3)             move to the parent until there is one or more next sibling elements
                *   (4)             if we reach the end break the loop
                */
                if (xElem->FirstChildElement()) //(1)
                    xElem = xElem->FirstChildElement();
                else if (xElem->NextSiblingElement())  //(2)
                    xElem = xElem->NextSiblingElement();
                else
                {
                    while(xElem->Parent() && !xElem->Parent()->NextSiblingElement()) //(3)
                        xElem = xElem->Parent();
                    if(xElem->Parent() && xElem->Parent()->NextSiblingElement())
                        xElem = xElem->Parent()->NextSiblingElement();
                    else //(4)
                        break;
                }//else
            }//while
    }

(for completeness) example how to call the function:

        tinyxml2::XMLDocument xXmlDocument;

        xXmlDocument.Parse(sXmlDocument.c_str());

        parseXML(xXmlDocument, "user",[](tinyxml2::XMLNode* xElem)
        {
            int iPass;
            xElem->QueryIntAttribute( "pass", &iPass );
            std::cout << iPass << "\n";
        });

Upvotes: 2

cake on the sky
cake on the sky

Reputation: 41

Try this

TiXmlElement * getElementByName(TiXmlDocument & doc, std::string const & elemt_value) {

   TiXmlElement * elem = doc.RootElement(); //Tree root
   while (elem) {
      if (!std::string(elem - > Value()).compare(elemt_value)) return (elem);
      /*elem = elem->NextSiblingElement();*/
      if (elem - > FirstChildElement()) {
         elem = elem - > FirstChildElement();
      } else if (elem - > NextSiblingElement()) {
         elem = elem - > NextSiblingElement();
      } else {
         while (!elem - > Parent() - > NextSiblingElement()) {
            if (elem - > Parent() - > ToElement() == doc.RootElement()) {
               return NULL;
            }
            elem = elem - > Parent() - > NextSiblingElement();
         }
      }
   }
   return (NULL);
}

Upvotes: 4

Related Questions