Search code examples
xmlxml-parsingc++buildertinyxmltmemo

How to get the value of an attribute and display it on a Tmemo


I have an xml file that looks like this...

<fruits>
    <apple color="red"/>
    <orange color="orange"/>
    <banana color="yellow"/>
</fruits>

I would like to take the value of the attribute color for each element, and display it on to a memo. I know how to display the value of an element on to a memo but I can't seem to figure out how to do it for an attribute. Here is my code so far...

TiXmlDocument XMLFile;
XMLFile.LoadFile("fruits.xml");

TiXmlHandle XMLFileHandle( &XMLFile );
TiXmlElement* root = XMLFile .FirstChildElement("fruits");

for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
{
    memoOverview->Lines->Add(elem->Attribute("val")->GetText());
}

I am using tinyxml for the parsing of the xml file, and I am doing this in C++ and C++ Builder.


Solution

  • TiXmlDocument XMLFile;
    XMLFile.LoadFile("fruits.xml");
    
    TiXmlHandle XMLFileHandle( &XMLFile );
    TiXmlElement* root = XMLFile.FirstChildElement("fruits");
    
    char stringBuffer[64];
    
    for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
    {
      if (strcmp(LastChildElement->Value(), "color") == 0)
      {
        strncpy(stringBuffer, LastChildElement->Attribute("color"), sizeof(stringBuffer));
      }
    
      memoOverview->Lines->Add( stringBuffer );
    }