0

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.

stukelly
  • 4,257
  • 3
  • 37
  • 44
livelaughlove
  • 376
  • 3
  • 9
  • 21
  • The code does not get syntax highlighting automatically, because you have not included a tag which the Google Code Prettify recognises. See [question 73082 on Meta](http://meta.stackexchange.com/questions/72082/changes-to-syntax-highlighting). You can specify the language of the code, by including a `` tag before the code block. See the [help](http://stackoverflow.com/editing-help#syntax-highlighting) for information. – stukelly Feb 25 '12 at 21:41

2 Answers2

0

According to the documentation, you need to replace elem->Attribute("val")->GetText() with elem->Attribute("color"):

memoOverview->Lines->Add(elem->Attribute("color"));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • it was meant to be posted with Attribute("color"). i posted the question using an example i created off the bat but forgot to change that part of the code. and for w/e reason it still wasn't working. what i ended up doing was copying the attribute as a string, and adding it onto the memo using that string – livelaughlove Feb 27 '12 at 14:47
0
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 );
}
stukelly
  • 4,257
  • 3
  • 37
  • 44
livelaughlove
  • 376
  • 3
  • 9
  • 21