C++ (GNU 컴파일러)에서 100,000 개의 직원 레코드가있는 이진 파일을 만들었습니다. 이제 C++을 사용하여 100,000 명의 직원 레코드가있는 XML 테이블을 작성해야합니다. 하지만 C++ 코드를 사용하여 XML 테이블을 작성하는 방법을 모르겠습니다. 이 프로그램을 수행 할 수있는 샘플 코드 또는 자습서가 있습니까?C++에서 XML 테이블 만들기
0
A
답변
0
는 여기에 사용자 지정 XML 형식으로 데이터를 기입하기위한 XML 직렬화 라이브러리를 사용하는 것이 좋습니다
#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;
}
+0
그냥 제쳐두고 - 누구든지이 투표를 잘못하면 어떻게 설명해주십시오 .-) –
+1
저는 유권자가 아니지만 어떤 분야도 제대로 벗어나지 못합니다. 문제의 전체 뗏목. 또한 XML의 "인코딩"에 관심을 기울이지 않습니다. –
0
간단한 인위적인 예에게 있습니다. 라이브러리 libstudxml는 낮은 수준의 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);
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를 제공 ++
예를 들어오픈 소스, MIT 라이센스 C는 직렬화 소스 있음을 언급 코드는 Genx (또는 MIT 라이선스가 있음)이라는 XML 직렬화를위한 작은 C 라이브러리에서 시작되었습니다.
XML 라이브러리를 선택하고 자습서를 작성해야합니다. "최고의 XML 라이브러리"에 대한 많은 질문이 있습니다. 10 초 동안 검색 한 결과 Windows 중심의 라이브러리가 생겼습니다. http://stackoverflow.com/questions/2990903/best-xml-library- in-c-fast-set-up -하지만 다른 OS를 사용하고 있다면 좀 더 검색 할 것을 제안합니다. 규칙을 벗어나는 올바른 값을 사용하고 있는지 확인하기 전에는 코드를 직접 작성하지 않도록 유혹을 피하십시오. –
XML과 관련하여 특별한 것이나 마법 같은 것은 데이터를 둘러싼 텍스트 일뿐입니다. –
가능한 복제본 [C++에서 사용하는 XML 구문 분석기] (http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c) –