Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create XML files from C++ program

Tags:

c++

I have this variable, a vector of double pointers like:

vector<double*> myIntersections;

which contains a vector whose elements are all a two dimensional vector of doubles. I want to create a XML file (called for example myfile.axl - with this specific extension) in which each row of the file is given by each element of the vector (so on each row one would have to elements at least vector[i][0], vector[i][1]) and the tags of the XML file are .., etc (so user defined). The file will XML should be something like:

<point name="intersectPoints" size="4" color="rgb">
  -2.68 1.82 0.0 255 0 0
  -2.63 1.03 0.0 255 0 0
</point>

where vector[0][0]=-2.68, vector[0][1]=1.82 and so on (0.0 255 0 0 being always the same) I know how to write file in C++ (I was thinking about using the fstream library), but I do not know how to create the XML tags (other than with strings in this way they will be though strings) so I am a little bit lost.

any suggestions is more than welcomed. thank you for your time, madalina

like image 514
madalina Avatar asked Mar 31 '09 15:03

madalina


1 Answers

Please. Please don't create XML on your own.

Use libraries which will generate valid and correct XML file.
Same things is related to reading XML. You aren't going to read XML with ifstream, are you? So if you have XML library to read XML files I am pretty sure that this library allows XML creation.

Here is sample code with 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" );  
}

Where 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;

and VertexPrint is

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_;
};

You can also consider boost XML serialization

like image 103
Mykola Golubyev Avatar answered Sep 22 '22 08:09

Mykola Golubyev