Example #1
0
//来自:TinyXML
void XMLElement::ToFile(FILE *file,int blankdepth)
{
	int i;
	for ( i = 0; i < blankdepth; i++ )
	{
		fprintf( file, "    " );
	}

	fprintf_s( file, "<%s", value.c_str() );

	XMLAttribute* attri = NULL;
	for ( attri = attributes.First(); attri; attri = attri->Next() )
	{
		fprintf_s( file, " " );
		attri->ToFile( file, blankdepth );
	}

	// There are 3 different formatting approaches:
	// 1) An element without children is printed as a <foo /> node
	// 2) An element with only a text child is printed as <foo> text </foo>
	// 3) An element with children is printed on multiple lines.
	
	if ( !firstChild )
	{
		fprintf_s( file, " />" );
	}
	else if ( firstChild == lastChild && firstChild->ToText() )
	{
		fprintf_s( file, ">" );
		firstChild->ToFile( file, blankdepth + 1 );
		fprintf_s( file, "</%s>", value.c_str() );
	}
	else
	{
		XMLNode* node = NULL;
		fprintf_s( file, ">" );
		for ( node = firstChild; node; node=node->NextSibling() )
		{
			if ( !node->ToText() )
			{
				fprintf_s( file, "\n" );
			}
			node->ToFile( file, blankdepth + 1 );
		}
		fprintf_s( file, "\n" );
		for( i=0; i < blankdepth; ++i )
			fprintf_s( file, "    " );
		fprintf_s( file, "</%s>", value.c_str() );
	}
}