пожалуйста. Пожалуйста, не создавайте XML самостоятельно.
Используйте библиотеки, которые будут генерировать действительный и правильный файл XML.
То же самое относится и к чтению XML. Вы не собираетесь читать XML с ifstream? Поэтому, если у вас есть библиотека XML для чтения файлов XML, я уверен, что эта библиотека позволяет создавать XML.
Вот пример кода с tinyxml
int main()
{
VertexList vl;
vl.push_back( Vertex( Point3d( -2.68, 1.82, 0.0 ), RGB( 255, 0, 0 )));
vl.push_back( Vertex( Point3d( -2.63, 1.03, 0.0 ), RGB( 255, 0, 0 )));
std::ostringstream ss;
std::for_each( vl.begin(), vl.end(), VertexPrint( ss ) );
// write xml
TiXmlDocument doc;
TiXmlDeclaration decl( "1.0", "", "" );
doc.InsertEndChild( decl );
TiXmlElement point( "point" );
TiXmlComment comment( "My Points" );
point.InsertEndChild( comment );
point.SetAttribute("name", "intersectPoints");
point.SetAttribute("size", vl.size());
point.SetAttribute("color", "rgb");
TiXmlText values( ss.str() );
point.InsertEndChild( values );
doc.InsertEndChild( point );
doc.SaveFile( "out.xml" );
}
Где Vertex
struct Point3d
{
Point3d( double x, double y, double z ):
x_(x), y_(y), z_(z)
{}
double x_;
double y_;
double z_;
};
struct RGB
{
RGB( int r, int g, int b ):
r_(r), g_(g), b_(b)
{}
int r_;
int g_;
int b_;
};
struct Vertex
{
Vertex( const Point3d& coord, const RGB& color ):
coord_( coord ), color_( color )
{}
Point3d coord_;
RGB color_;
};
typedef std::vector< Vertex > VertexList;
и VertexPrint
- это
struct VertexPrint
{
VertexPrint( std::ostringstream& result ):
result_( result )
{}
void operator() ( const Vertex& v )
{
result_ << v.coord_.x_ <<" "<< v.coord_.y_ <<" "<< v.coord_.z_ <<" "
<< v.color_.r_ <<" "<< v.color_.b_ <<" "<< v.color_.b_ <<";";
}
std::ostringstream& result_;
};
Вы также можете рассмотреть повысить сериализацию XML