簡體   English   中英

從 C++ 程序創建 XML 文件

[英]create XML files from C++ program

我有這個變量,一個雙指針向量,如:

vector<double*> myIntersections;

其中包含一個向量,其元素都是雙精度的二維向量。 我想創建一個 XML 文件(例如稱為 myfile.axl - 具有此特定擴展名),其中文件的每一行由向量的每個元素給出(因此在每一行上都必須至少有向量 [i ][0], vector[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>

其中vector[0][0]=-2.68,vector[0][1]=1.82 等等(0.0 255 0 0 始終相同)我知道如何在 C++ 中寫入文件(我正在考慮使用 fstream庫),但我不知道如何創建 XML 標簽(除了以這種方式使用字符串之外,它們將是字符串)所以我有點迷失了。

任何建議都非常受歡迎。 謝謝你的時間,馬達琳娜

請。 請不要自行創建 XML。

使用將生成有效且正確的 XML 文件的庫。
同樣的事情與閱讀 XML 有關。 你不會用 ifstream 來閱讀 XML 吧? 因此,如果您有 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_;
};

也可以考慮boost XML 序列化

查看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. 您還可以使用 C++ 對象從頭開始構建 XML 文檔,並將其寫入磁盤或另一個 output ZF7B44CAFFD5C68A223

我在自己的項目中使用過 TinyXml,使用起來很愉快。

編輯:除了 TinyXML,我還使用了 ticpp (TinyXml++),它在庫頂部引入了 C++ 的更多功能(異常、模板、迭代器等)。

如果你只是在寫 XML,那么一個成熟的解析器就有點過頭了。 最好看看像Genx這樣的東西

我使用 xerces 生成 XML 文件,並且在 memory 和 CPU 使用率方面非常昂貴。 我最終得到了“內存不足”異常,試圖生成只有幾十萬行 XML 的文件。 如果您可以像其他一些答案所建議的那樣直接生成它,並且正如您的問題所暗示的那樣,我會使用 go 這條路線。 讀取和解析是另一回事,但使用 xerces 生成 XML 是多余的。

假設您使用的是 Windows 並使用 Visual Studio,您可以使用MSXML或其他第 3 方庫。

看一個黑客...

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

這就是我用很少的開銷編寫 xml 文件的方式,只需將原始 xml 保存到文件中。

要讀取和寫入 xml 文件,您通常會使用xml 解析器(基於 sax 或 dom)

我使用的是一個名為xerces的 sax 解析器

我現在正在看 TinyXml。 我已經下載了,希望可以安裝。 我使用 MacOSX,我可以看到 Linux 的 Makefile 也已成功測試 MacOSX,因此我將嘗試安裝它,如果成功安裝則可以使用它。

對於像這樣簡單的事情,直接編寫標簽不會很復雜:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM