linux tinyxml遍历

方法一、

 

bool ReadElemnt(string& szFileName)
{
 TiXmlDocument myDocument(szFileName);   
  bool loadOkay = myDocument.LoadFile();
  if(loadOkay == false)
   return false;
 TiXmlElement *rootElement = myDocument.RootElement();
 if(rootElement == NULL)
  return false;
 
 TiXmlElement *pItemElement = rootElement->FirstChildElement();
 TiXmlNode *pNextchild = pItemElement->ToElement();
 while(pNextchild)
 {
  pItemElement = pNextchild->ToElement();  
  
    TiXmlElement *NameElement = pItemElement->FirstChildElement();
    TiXmlElement *AgeElement = NameElement->NextSiblingElement();
    TiXmlAttribute *IDAttribute = pItemElement->FirstAttribute();       
    //输出第一个Person的name内容,即周星星;age内容,即20;ID属性,即1。     
  cout << NameElement->FirstChild()->Value() << "<br>";         
    cout << AgeElement->FirstChild()->Value() << "<br>";
    cout << IDAttribute->Value() << "<br>/n";
    
    pNextchild = pNextchild->NextSibling();
 }
 
 return true;
}

 

 

 

方法二、

void ParaseUpdateXml(TiXmlNode* pParent)
{
  if(pParent == NULL)
  return;        

  TiXmlNode* pChild = pParent->FirstChild();    
  while(pChild)
  {          
    cout << pChild->Value() << "<br>";
    int t = pChild->Type();
    if( t == TiXmlNode::ELEMENT)
    {   
      TiXmlAttribute *attr = pChild->ToElement()->FirstAttribute();        
      if(attr)
      {                    
        TiXmlNode* node = pChild;
        while(node)
        {
          while(attr)                        
          {                       
            //cout << " " << attr->Name() << "=" << attr->Value() << "###<br>";
            attr = attr->Next();
          }       
          //cout << endl;                   
          node =  node->NextSiblingElement();       
        }                                                 
      }           
      ParaseUpdateXml(pChild);
    
    }                 
    else if( t == TiXmlNode::TEXT)
    {              
      //cout << pChild->Value() << "@@@<br>";                            
    }      
    
    pChild = pChild->NextSibling();  
  } 
}

你可能感兴趣的:(linux tinyxml遍历)