Example #1
0
void TiXmlElement::Print( FILE* cfile, int depth ) const
{
	int i;
	for ( i=0; i<depth; i++ )
	{
		generic_fprintf( cfile, TEXT("    ") );
	}

	generic_fprintf( cfile, TEXT("<%s"), value.c_str() );

	TiXmlAttribute* attrib;
	for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() )
	{
		generic_fprintf( cfile, TEXT(" ") );
		attrib->Print( cfile, depth );
	}

	// 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.
	TiXmlNode* node;
	if ( !firstChild )
	{
		generic_fprintf( cfile, TEXT(" />") );
	}
	else if ( firstChild == lastChild && firstChild->ToText() )
	{
		generic_fprintf( cfile, TEXT(">") );
		firstChild->Print( cfile, depth + 1 );
		generic_fprintf( cfile, TEXT("</%s>"), value.c_str() );
	}
	else
	{
		generic_fprintf( cfile, TEXT(">") );

		for ( node = firstChild; node; node=node->NextSibling() )
		{
			if ( !node->ToText() )
			{
				generic_fprintf( cfile, TEXT("\n") );
			}
			node->Print( cfile, depth+1 );
		}
		generic_fprintf( cfile, TEXT("\n") );
		for( i=0; i<depth; ++i )
		generic_fprintf( cfile, TEXT("    ") );
		generic_fprintf( cfile, TEXT("</%s>"), value.c_str() );
	}
}
Example #2
0
void TiXmlElement::Print( FILE* fp, int depth )
{
	int i;
	for ( i=0; i<depth; i++ )
		fprintf( fp, "    " );

	fprintf( fp, "<%s", value.c_str() );

	TiXmlAttribute* attrib;
	for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() )
	{	
		fprintf( fp, " " );
		attrib->Print( fp, 0 );
	}
	// If this node has children, give it a closing tag. Else
	// make it an empty tag.
	TiXmlNode* node;
	if ( firstChild )
	{ 		
		fprintf( fp, ">" );

		for ( node = firstChild; node; node=node->NextSibling() )
		{
	 		if ( !node->ToText() )
				fprintf( fp, "\n" );
			node->Print( fp, depth+1 );
		}
 		fprintf( fp, "\n" );
		for ( i=0; i<depth; i++ )
			fprintf( fp, "    " );
		fprintf( fp, "</%s>", value.c_str() );
	}
	else
	{
		fprintf( fp, " />" );
	}
}