从c++程序创建XML文件

时间:2021-01-13 15:08:26

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:

它包含一个向量,它的元素都是双键的二维向量。我想要创建一个XML文件(例如myfile)。axl——有了这个特定的扩展名),文件的每一行都由向量的每个元素给出(因此在每一行中,至少有一个元素是向量[i][0],向量[i][1]), XML文件的标签是。等(用户定义)。XML文件应该是这样的:

<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.

在向量[0][0]= -2.68,向量[0][1]= 1.82等等(0.0 255 0 0都一样)我知道如何编写文件在c++中(我在想使用fstream库),但我不知道如何创建XML标记(除了与字符串以这种方式他们将虽然字符串)所以我有点迷路了。

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

任何建议都非常受欢迎。谢谢你的时间,玛德琳娜

9 个解决方案

#1


7  

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

请。请不要自己创建XML。

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.

使用将生成有效和正确的XML文件的库。同样的事情也与读取XML有关。您不打算使用ifstream读取XML,是吗?因此,如果您有XML库来读取XML文件,我很确定这个库允许XML创建。

Here is sample code with tinyxml

下面是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

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

You can also consider boost XML serialization

您还可以考虑boost XML序列化

#2


5  

Check out TinyXml. It's extremely lightweight. From the documentation:

TinyXml。它是非常轻量级的。从文档:

TinyXML uses a Document Object Model (DOM), meaning the XML data is parsed into a C++ objects that can be browsed and manipulated, and then written to disk or another output stream. You can also construct an XML document from scratch with C++ objects and write this to disk or another output stream.

TinyXML使用文档对象模型(Document Object Model, DOM),这意味着XML数据被解析为c++对象,可以对其进行浏览和操作,然后写入磁盘或其他输出流。您还可以使用c++对象从头构建XML文档,并将其写入磁盘或其他输出流。

I've used TinyXml in my own projects and it is a joy to use.

我在自己的项目中使用了TinyXml,使用起来很愉快。

Edit: In addition to TinyXML, I also use ticpp (TinyXml++) which introduces more features of C++ (exceptions, templates, iterators, etc) on top of the library.

编辑:除了TinyXML之外,我还使用了ticpp (TinyXML ++),它在库的顶部引入了c++的更多特性(异常、模板、迭代器等)。

#3


1  

If you are just writing XML, a full blown parser is an overkill. Better take a look at something like Genx

如果您只是在编写XML,那么完整的解析器就太过分了。最好看看Genx之类的东西。

#4


1  

I have used xerces to generate XML files, and it very expensive in terms of memory and CPU usage. I eventually got "out of memory" exceptions trying to generate files with only a few hundreds of thousands of lines of XML. If you can get away with generating it directly as some other answers have suggested, and as your question implies should be possible, I would go that route. Reading and parsing is a different story, but using xerces to generate XML is overkill.

我已经使用xerces生成XML文件,并且在内存和CPU使用方面非常昂贵。我最终得到了“内存之外”的异常,试图生成只有几十万行XML的文件。如果你能像其他答案所建议的那样直接生成它,而且你的问题也暗示着这是可能的,我就会走那条路。读取和解析是另一回事,但是使用xerces生成XML就太过分了。

#5


0  

Assuming you are on Windows and using Visual Studio, you could use MSXML or other 3rd party libraries.

假设您在Windows上并使用Visual Studio,您可以使用MSXML或其他第三方库。

#6


0  

behold a hack...

看哪一个黑客…

cout << "<point name=\"intersectPoints\" size=\"4\" color=\"rgb\">"<<endl;
cout << "  "<<-2.68<<" "<<1.82<<" "<<0.0<<" "<<255<<" "<<0<<" "<<0<<"";

that's how i write xml files with little overhead just save to the file the raw xml.

这就是我编写xml文件的方式,只需要很少的开销就可以将原始xml保存到文件中。

#7


0  

To read and write xml files you would usually use an xml parser (sax or dom based)

要读写xml文件,通常需要使用xml解析器(基于sax或dom)

One I have used is a sax parser called xerces

我使用的一个是sax解析器xerces

#8


0  

I am looking at TinyXml now. I have downloaded it I hope I can install it. I use MacOSX, I can see the Makefile for Linux was also successfully tested for MacOSX so I will try to install it and if successfully installed worked with it.

我现在看到的是TinyXml。我已经下载了,我希望我能安装它。我使用MacOSX,我可以看到Linux的Makefile也成功地测试了MacOSX,所以我将尝试安装它,如果成功安装,我将使用它。

#9


0  

For something simple like this, writing the tags directly would not be very complicated:

对于像这样简单的东西,直接写标签不会很复杂:

void WriteToFile(fstream& file, vector<double *> intersections)
{
    file << "<point";
    file << " name=\"intersectPoints\"";
    file << " size=\"" << intersections.size() "\"";
    file << " color=\"rgb\""
    file << ">\n"

    vector<double *>::iterator it;
    for (it=intersections.begin(); it != intersections.end(); it++)
    {
        file << (*it)[0] << " " << (*it)[1] << " " << (*it)[2] << " ";
        file << "255 0 0\n";
    }

    file << "</point>" << endl;
}

#1


7  

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

请。请不要自己创建XML。

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.

使用将生成有效和正确的XML文件的库。同样的事情也与读取XML有关。您不打算使用ifstream读取XML,是吗?因此,如果您有XML库来读取XML文件,我很确定这个库允许XML创建。

Here is sample code with tinyxml

下面是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

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

You can also consider boost XML serialization

您还可以考虑boost XML序列化

#2


5  

Check out TinyXml. It's extremely lightweight. From the documentation:

TinyXml。它是非常轻量级的。从文档:

TinyXML uses a Document Object Model (DOM), meaning the XML data is parsed into a C++ objects that can be browsed and manipulated, and then written to disk or another output stream. You can also construct an XML document from scratch with C++ objects and write this to disk or another output stream.

TinyXML使用文档对象模型(Document Object Model, DOM),这意味着XML数据被解析为c++对象,可以对其进行浏览和操作,然后写入磁盘或其他输出流。您还可以使用c++对象从头构建XML文档,并将其写入磁盘或其他输出流。

I've used TinyXml in my own projects and it is a joy to use.

我在自己的项目中使用了TinyXml,使用起来很愉快。

Edit: In addition to TinyXML, I also use ticpp (TinyXml++) which introduces more features of C++ (exceptions, templates, iterators, etc) on top of the library.

编辑:除了TinyXML之外,我还使用了ticpp (TinyXML ++),它在库的顶部引入了c++的更多特性(异常、模板、迭代器等)。

#3


1  

If you are just writing XML, a full blown parser is an overkill. Better take a look at something like Genx

如果您只是在编写XML,那么完整的解析器就太过分了。最好看看Genx之类的东西。

#4


1  

I have used xerces to generate XML files, and it very expensive in terms of memory and CPU usage. I eventually got "out of memory" exceptions trying to generate files with only a few hundreds of thousands of lines of XML. If you can get away with generating it directly as some other answers have suggested, and as your question implies should be possible, I would go that route. Reading and parsing is a different story, but using xerces to generate XML is overkill.

我已经使用xerces生成XML文件,并且在内存和CPU使用方面非常昂贵。我最终得到了“内存之外”的异常,试图生成只有几十万行XML的文件。如果你能像其他答案所建议的那样直接生成它,而且你的问题也暗示着这是可能的,我就会走那条路。读取和解析是另一回事,但是使用xerces生成XML就太过分了。

#5


0  

Assuming you are on Windows and using Visual Studio, you could use MSXML or other 3rd party libraries.

假设您在Windows上并使用Visual Studio,您可以使用MSXML或其他第三方库。

#6


0  

behold a hack...

看哪一个黑客…

cout << "<point name=\"intersectPoints\" size=\"4\" color=\"rgb\">"<<endl;
cout << "  "<<-2.68<<" "<<1.82<<" "<<0.0<<" "<<255<<" "<<0<<" "<<0<<"";

that's how i write xml files with little overhead just save to the file the raw xml.

这就是我编写xml文件的方式,只需要很少的开销就可以将原始xml保存到文件中。

#7


0  

To read and write xml files you would usually use an xml parser (sax or dom based)

要读写xml文件,通常需要使用xml解析器(基于sax或dom)

One I have used is a sax parser called xerces

我使用的一个是sax解析器xerces

#8


0  

I am looking at TinyXml now. I have downloaded it I hope I can install it. I use MacOSX, I can see the Makefile for Linux was also successfully tested for MacOSX so I will try to install it and if successfully installed worked with it.

我现在看到的是TinyXml。我已经下载了,我希望我能安装它。我使用MacOSX,我可以看到Linux的Makefile也成功地测试了MacOSX,所以我将尝试安装它,如果成功安装,我将使用它。

#9


0  

For something simple like this, writing the tags directly would not be very complicated:

对于像这样简单的东西,直接写标签不会很复杂:

void WriteToFile(fstream& file, vector<double *> intersections)
{
    file << "<point";
    file << " name=\"intersectPoints\"";
    file << " size=\"" << intersections.size() "\"";
    file << " color=\"rgb\""
    file << ">\n"

    vector<double *>::iterator it;
    for (it=intersections.begin(); it != intersections.end(); it++)
    {
        file << (*it)[0] << " " << (*it)[1] << " " << (*it)[2] << " ";
        file << "255 0 0\n";
    }

    file << "</point>" << endl;
}