0

C++ (GNU コンパイラ) で 100,000 の Employee レコードを含むバイナリ ファイルを作成しました。ここで、C++ を使用して、その 100,000 個の従業員レコードを含む XML テーブルを作成する必要があります。しかし、C++ コードを使用して XML テーブルを作成する方法がわかりません。このプログラムを実行するためのサンプル コードやチュートリアルはありますか?

4

2 に答える 2

0

これは単純な考案された例です

#include <iostream>
#include <vector>
#include <string>

class Employee
{
   public:
      Employee(const std::string &firstname, const std::string &lastname, int salary)
         :firstname_(firstname), lastname_(lastname), salary_(salary)
      {
      }
      friend std::ostream &operator<<(std::ostream &os, const Employee &rhs)
      {
         rhs.print(os);
         return os;
      }
   private:
      void print(std::ostream &os) const
      {
         os << "<employee>";
         os << "<firstname>" << firstname_ << "</firstname>";
         os << "<lastname>" << lastname_ << "</lastname>";
         os << "<salary>" << salary_ << "</salary>";
         os << "</employee>\n";
      }
      std::string firstname_;
      std::string lastname_;
      int salary_;
};

int main(int argc, char *argv[])
{
   std::vector<Employee> staff;

   staff.push_back(Employee("Peter", "Griffin", 10000));
   staff.push_back(Employee("Lois", "Griffin", 20000));
   staff.push_back(Employee("Chris", "Griffin", 30000));
   staff.push_back(Employee("Meg", "Griffin", 40000));
   staff.push_back(Employee("Stewie", "Griffin", 50000));
   staff.push_back(Employee("Brian", "Griffin", 60000));

   std::cout << "<staff>\n";
   for(std::vector<Employee>::const_iterator i=staff.begin(),end=staff.end(); i!=end; ++i)
   {
      std::cout << (*i);
   }
   std::cout << "</staff>\n";

   return 0;
}
于 2012-06-23T05:55:26.223 に答える
0

データをカスタム XML 形式に書き込むには、XML シリアル化ライブラリを使用することをお勧めします。

たとえば、オープン ソースの MIT ライセンス C++ ライブラリlibstudxmlは、低レベル API と

void start_element (const std::string& name);
void end_element ();
void start_attribute (const std::string& name);
void end_attribute ();
void characters (const std::string& value);

および高レベル API

template <typename T>
  void element (const T& value);
template <typename T>
  void characters (const T& value);
template <typename T>
  void attribute (const std::string& name,
      const T& value);

libstudxmlのドキュメントには、シリアライゼーション ソース コードの起源が、Genxと呼ばれる XML シリアライゼーション用の小さな C ライブラリにあることが記載されています(これも MIT ライセンス)。

于 2015-07-17T15:28:50.360 に答える