XML Parser(Tinyxml)的使用

1. 下载开源代码Tinyxml,附件带有.
2. 直接将源码加入到我们的工程中.
3. 包含相应的头文件.
4. 测试代码
Read :
TiXmlDocument * myDocument    
                 = new TiXmlDocument("d:\\test.xml");    

myDocument->LoadFile();    
        
TiXmlElement *RootElement = myDocument->RootElement();    
cout << RootElement->Value() << endl;    
                    
TiXmlElement *Tips = RootElement->FirstChildElement();    
cout << Tips->Value() << endl;    

TiXmlElement *Tips1 = Tips->FirstChildElement();    
TiXmlElement *Tips2 = Tips1->NextSiblingElement();    
TiXmlElement *Tips3 = Tips2->NextSiblingElement();    
TiXmlElement *Tips4 = Tips3->NextSiblingElement();    
TiXmlElement *Tips5 = Tips4->NextSiblingElement();    
                    
cout << Tips1->Value() << endl;    
cout << Tips2->Value() << endl;    
cout << Tips3->Value() << endl;    
cout << Tips4->Value() << endl;    
cout << Tips5->Value() << endl;    

//Tips1    
TiXmlAttribute * Tips1_Attr1 = Tips2->FirstAttribute();    
while(Tips1_Attr1)    
{    
                cout << Tips1_Attr1->Name() << " = "    
                             << Tips1_Attr1->Value() << endl ;    

                Tips1_Attr1 = Tips1_Attr1->Next();    
}    

TiXmlElement *Tips11 = Tips2->FirstChildElement();    
cout << Tips11->GetText() << endl;    

delete myDocument;


Write:
TiXmlDocument * myDocument        =    
                     new TiXmlDocument("d:\\test2.xml");    

TiXmlElement *RootElement = new TiXmlElement("xlTips") ;    
myDocument->LinkEndChild(RootElement);    

TiXmlElement *Tips = new TiXmlElement("Tips") ;    
RootElement->LinkEndChild(Tips);    
        
TiXmlElement *Tips1 = new TiXmlElement("Tip1") ;    
TiXmlElement *Tips2 = new TiXmlElement("Tip2") ;    
TiXmlElement *Tips3 = new TiXmlElement("Tip3") ;    
TiXmlElement *Tips4 = new TiXmlElement("Tip4") ;    
TiXmlElement *Tips5 = new TiXmlElement("Tip5") ;    

Tips->LinkEndChild(Tips1);    
Tips->LinkEndChild(Tips2);    
Tips->LinkEndChild(Tips3);    
Tips->LinkEndChild(Tips4);    
Tips->LinkEndChild(Tips5);    

Tips1->SetAttribute("id1","111");    
Tips1->SetAttribute("ver1","11");    
Tips1->SetAttribute("type1","1");    

Tips2->SetAttribute("id2","222");    
Tips2->SetAttribute("ver2","22");    
Tips2->SetAttribute("type2","2");    

Tips3->SetAttribute("id3","333");    
Tips3->SetAttribute("ver3","33");    
Tips3->SetAttribute("type3","3");    

Tips4->SetAttribute("id4","444");    
Tips4->SetAttribute("ver4","44");    
Tips4->SetAttribute("type4","4");    

Tips5->SetAttribute("id5","555");    
Tips5->SetAttribute("ver5","55");    
Tips5->SetAttribute("type5","5");    

TiXmlElement *Tips11 = new TiXmlElement("htmlurl") ;    
Tips1->LinkEndChild(Tips11);    

TiXmlText * Tips111 = new TiXmlText("Test");    
Tips11->LinkEndChild(Tips111);    
Tips111->SetCDATA(true);    

myDocument->SaveFile();    
delete myDocument;
 

你可能感兴趣的:(xml,职场,休闲,TinyXML )