2012-01-03 3 views
1

RapidXML의 콘텐츠를 xml 파일에 저장하려면 어떻게해야합니까? 내 응용 프로그램은 MFC이고 문자열을 조작하기가 더 쉽기 때문에 CString을 많이 사용합니다. 그래서 아래 코드가 있다면, 어떻게 xml 파일에 저장할 수 있습니까?MFC의 파일에 RapidXML 콘텐츠를 쓰려면 어떻게해야합니까?

#include "rapidxml.hpp" 
rapidxml::xml_document<> doc; 
rapidxml::xml_node<>* decl = doc.allocate_node(rapidxml::node_type::node_declaration, 0,0,0,0); 
decl->append_attribute(doc.allocate_attribute("xml version", "1.0")); 
doc.append_node(decl); 

rapidxml::xml_node<>* comment = doc.allocate_node(rapidxml::node_type::node_comment, 0, "Application Config"); 
doc.append_node(comment); 

rapidxml::xml_node<>* subsytem = doc.allocate_node(rapidxml::node_type::node_element, "Subsystem"); 
subsytem->append_attribute(doc.allocate_attribute("Name", "My App")); 
subsytem->append_attribute(doc.allocate_attribute("Version", "1.0")); 
doc.append_node(subsytem); 

감사합니다. (http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1printing) 매우 분명하게 존재하는 문제에 대한 솔루션 번호 RapidXML 설명서의

+1

2.4 절 인쇄 XML. –

답변

3
using namespace rapidxml; 
int testmain() 
{ 
    xml_document<> doc; 
    xml_node<>* decl = doc.allocate_node(node_declaration); 
    decl->append_attribute(doc.allocate_attribute("version", "1.0")); 
    decl->append_attribute(doc.allocate_attribute("encoding", "UTF-8")); 
    doc.append_node(decl); 



    xml_node<> *files = doc.allocate_node(node_element, "Files"); 
    doc.append_node(files); 
    xml_attribute<> *attr = doc.allocate_attribute("dummy", "google.com"); 
    files->append_attribute(attr); 

    for(int i = 0;i<10;++i) 
    { 
     xml_node<> *file = doc.allocate_node(node_element, "File"); 
     files->append_node(file); 

     xml_node<> *path = doc.allocate_node(node_element, "Path","File_path"); 
     file->append_node(path); 
     xml_node<> *name = doc.allocate_node(node_element, "Name""File_name"); 
     file->append_node(name); 
    } 

    std::ofstream myfile; 
    myfile.open ("c:/example.xml"); 
    myfile << doc; 
    //print(std::cout, doc, 0); 
    return 0; 
}; 
+1

컴파일하기 위해'myfile << doc'에 #include "rapidxml_print.hpp"가 있는지 확인하십시오. – acraig5075

관련 문제