TinyXML简单的生成和解析示例

#include 
#include 
using namespace std;

#include "../tinyxml/tinyxml.h"

int test1()
{
	TiXmlDocument xml_doc;
	/*建立*/
	//a
	TiXmlElement* xmlElemA = new TiXmlElement("A");
	TiXmlText* xmlTextA = new TiXmlText("a");
	xmlElemA->LinkEndChild(xmlTextA);

	//b
	TiXmlElement* xmlElemB = new TiXmlElement("B");
	TiXmlText* xmlTextB = new TiXmlText("b");
	xmlElemB->LinkEndChild(xmlTextB);

	//c
	TiXmlElement* xmlElemC = new TiXmlElement("C");
	TiXmlText* xmlTextC = new TiXmlText("c");
	xmlElemC->LinkEndChild(xmlTextC);

	xml_doc.LinkEndChild(xmlElemA);
	xml_doc.LinkEndChild(xmlElemB);
	xml_doc.LinkEndChild(xmlElemC);

	// 保存到文件	
	xml_doc.SaveFile("abc.xml");
	
	
	/*解析*/
	//从文件中读取
	TiXmlDocument xml_doc2;
	if (!xml_doc2.LoadFile("abc.xml"))
	{
		return -1;
	}

	TiXmlNode* node1 = xml_doc2.FirstChild("A");
	TiXmlNode* node2 = xml_doc2.FirstChild("B");
	TiXmlNode* node3 = xml_doc2.FirstChild("C");

	cout << "node1: " << *node1 << endl;
	cout << "node2: " << *node2 << endl;
	cout << "node3: " << *node3 << endl;
	
	return 0;
}


int main()
{

	test1();
	return 0;
}

生成的XML文件如下:

a
b
c

常见操作:

#include "tinyxml.h"
#include 
using namespace std;
 
TiXmlDocument *pDoc =NULL;
void write_xml( )/*写XMl文档的函数*/
    {
 
        TiXmlDocument doc;
        TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
        doc.LinkEndChild( decl );
 
        TiXmlElement * element1 = new TiXmlElement( "AMULE_ToDL" );//创建元素element1
        element1->SetAttribute("num",5);
        doc.LinkEndChild( element1 );
//      TiXmlText * text = new TiXmlText( "World" );//创建一个文本
//      element->LinkEndChild( text );//链接
 
 
        TiXmlElement * element11 = new TiXmlElement( "name" );
        element11->SetAttribute("name","GongFong.rmvb");
        element1->LinkEndChild( element11 );//链接element11为element1的子节点(子元素)
 
        TiXmlElement * element2 = new TiXmlElement( "BT_ToDL" );
        element2->SetAttribute("num",10);//创建属性
        doc.LinkEndChild(element2);
    //dump_to_stdout(&doc);
        doc.SaveFile( "1.xml" );
}
 
 
void dump_to_stdout( TiXmlNode* pParent )//Tixml主页上给的一个遍历方法
{
        if ( !pParent ) return;
 
        TiXmlNode* pChild;
        TiXmlText* pText;
        int t = pParent->Type();
        printf( "type %d/n", t);
        int num;
 
        switch ( t )
        {
        case TiXmlNode::DOCUMENT:
               printf( "Document" );
                break;
 
        case TiXmlNode::ELEMENT:
                printf( "Element [%s]", pParent->Value() );
               num=dump_attribs_to_stdout(pParent->ToElement(), indent+1);
                switch(num)
                {
                        case 0:  printf( " (No attributes)"); break;
                        case 1:  printf( "%s1 attribute", getIndentAlt(indent)); break;
                        default: printf( "%s%d attributes", getIndentAlt(indent), num); break;
                }
                break;
 
        case TiXmlNode::COMMENT:
               printf( "Comment: [%s]", pParent->Value());
                break;
 
        case TiXmlNode::UNKNOWN:
                printf( "Unknown" );
                break;
 
        case TiXmlNode::TEXT:
               pText = pParent->ToText();
                printf( "Text: [%s]", pText->Value() );
                break;
 
        case TiXmlNode::DECLARATION:
                printf( "Declaration" );
                break;
        default:
                break;
        }
        printf( "/n" );
        for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
        {
                dump_to_stdout( pChild );
        }
}
void search(TiXmlNode* pParent)//遍历时候,把每个节点都是做一个父节点,即假定其都有子节点ChildNode
{
       if ( !pParent ) return;
       
       TiXmlNode* pChild= NULL;
           int t = pParent->Type();//获取此节点的类型
 
       if (TiXmlNode::ELEMENT == t && (strcmp("Welcome",pParent->Value()) == 0))//搜索元素值为"Welcome"的
        {
           printf("value %s/n",pParent->Value());//打印值
           pParent->SetValue("re-write");//改变其属性
           pParent->ToElement()->SetAttribute("hello",20);
 
           TiXmlElement * element1 = new TiXmlElement( "AMULE_ToDL" );//添加一个节点
               element1->SetAttribute("num",5);
               pParent->LinkEndChild(element1);
 
           pDoc->SaveFile(); //保存文件              
        }
    printf( "/n" );
    for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
        {
            search(pChild);
    }
      
}
void search2(TiXmlNode* pParent)//另一种方法:
{
       if ( !pParent ) return;
       
       TiXmlNode* pChild= NULL;
       TiXmlNode*tmp = NULL;
           int t = pParent->Type();
 
       tmp = pParent->FirstChild("Window");//搜索元素值为"Window"的节点
       if(tmp){
        
      //pParent->RemoveChild(tmp);//找到后删除此节点
            
       TiXmlElement * element1 = new TiXmlElement( "AMULE_ToDL" );
           element1->SetAttribute("num",5);
 
       pParent->ReplaceChild(tmp,*element1);//找到"AMULE_ToDL"节点后替换此节点
       
       }
 
       pDoc->SaveFile(); //保存文件               
        
    printf( "/n" );
    for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) //遍历一个节点的所兄弟节点,记着单词sibling是兄弟, 姐妹的意思
                                                                                                                                //也就是说FirstChild()和NextSibling()不是 父节点和子节点的关系
    {
            search2(pChild);//遍历一个节点的所有子节点
    }
      
}
int main(int argc, char* argv[])
   {
 
        TiXmlDocument doc(argv[1]);
        bool loadOkay = doc.LoadFile();
        if (loadOkay)
        {
            pDoc = &doc;
                printf("/n%s:/n", argv[1]);
                //dump_to_stdout( &doc ); // defined later in the tutorial
                search2(&doc);                //把TiXmlDocument类型的值复制给一个TiXmlNode类型
        }
        else
        {
                printf("Failed to load file /"%s/"/n", argv[1]);
        }
  
     return 0;
   }
 
/*void dump_to_stdout1(const char* pFilename)
{
        TiXmlDocument doc(pFilename);
        bool loadOkay = doc.LoadFile();
        if (loadOkay)
        {
                printf("/n%s:/n", pFilename);
                dump_to_stdout( &doc ); // defined later in the tutorial
        }
        else
        {
                printf("Failed to load file /"%s/"/n", pFilename);
        }
int main(int argc, char* argv[])
{
        for (int i=1; i

 

你可能感兴趣的:(C++)