tinyxml的使用

	TiXmlDocument doc;
	TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
	TiXmlElement * element = new TiXmlElement( "Hello" );
	TiXmlElement * element2 = new TiXmlElement( "Hello" );
	TiXmlElement * row = new TiXmlElement( "row" );
	TiXmlElement * row2 = new TiXmlElement( "row" );
	TiXmlText *text = new TiXmlText( "World" );
	TiXmlText * text2 = new TiXmlText( "World2" );
	element->LinkEndChild( text );
	element2->LinkEndChild( text2 );

	TiXmlElement * content = new TiXmlElement( "content" );

	row->LinkEndChild(element);
	row2->LinkEndChild(element2);
	
	content->LinkEndChild( row );
	content->LinkEndChild( row2 );
	doc.LinkEndChild( decl );
	doc.LinkEndChild( content );
	doc.SaveFile( "example.xml" );




<?xml version="1.0" ?>
<content>
    <row>
        <Hello>World</Hello>
    </row>
    <row>
        <Hello>World2</Hello>
    </row>
</content>




const char* filepath = "example.xml";   
	TiXmlDocument doc(filepath);   
	bool loadOkay = doc.LoadFile();   
	// faile to load 'phonebookdata.xml'.   
	if (!loadOkay) {       
		cout<<"not loadOkay"  <<endl;
		return 0;
	}   

 
	TiXmlElement* root = doc.RootElement(); 
	
	for( TiXmlNode*  item = root->FirstChild( "row" ); item;item = item->NextSibling( "row" ) ) {
		TiXmlNode* child = item->FirstChild();   
		const char* name = child->ToElement()->GetText();   
		if (name) {   
			cout<<"Value:"<<name<<endl;   
		} else {   
			cout<<"Value:"<< endl;    
		}   

	}




const char* filepath = "example.xml";   
	TiXmlDocument doc(filepath);   
	bool loadOkay = doc.LoadFile();   
	// faile to load 'phonebookdata.xml'.   
	if (!loadOkay) {       
		cout<<"not loadOkay"  <<endl;
		return 0;
	}   
   
	//TiXmlElement* root = doc.RootElement(); 
	

	TiXmlElement *root = doc.FirstChildElement();


	for( TiXmlNode*  item = root->FirstChild( "row" ); item;item = item->NextSibling( "row" ) ) {
		TiXmlNode* child = item->FirstChild();   
		const char* name = child->ToElement()->GetText();   
		if (name) {   
			cout<<"Value:"<<name<<endl;   
		} else {   
			cout<<"Value:"<< endl;    
		}   

	}




	const char* filepath = "example.xml";   
	TiXmlDocument doc(filepath);   
	bool loadOkay = doc.LoadFile();   
 
	// faile to load 'phonebookdata.xml'.   
	if (!loadOkay) {       
		cout<<"not loadOkay"  <<endl;
		//return 0;
	}   
   
	//TiXmlElement* root = doc.RootElement(); 
	

	TiXmlElement *root = doc.FirstChildElement();
	TiXmlHandle *docHandle = new TiXmlHandle (root);  //new line

	for( TiXmlNode*  item = docHandle->FirstChild( "row" ).ToElement(); item;item = item->NextSibling( "row" ) ) {
		TiXmlNode* child = item->FirstChild();   
		const char* name = child->ToElement()->GetText();   
		if (name) {   
			cout<<"Value:"<<name<<endl;   
		} else {   
			cout<<"Value:"<< endl;    
		}   

	}


Value:World
Value:World2

你可能感兴趣的:(C++,c,xml,C#)