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.

4

2 回答 2

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 );
}
于 2012-02-27T14:59:07.903 回答
0

根据文档,您需要替换elem->Attribute("val")->GetText()elem->Attribute("color")

memoOverview->Lines->Add(elem->Attribute("color"));
于 2012-02-25T06:47:26.470 回答