2011-07-04 2 views
27

내 응용 프로그램에서 XML 구성 파일을 읽고 쓰려면 boost :: property_tree를 사용하고 있습니다. 하지만 파일을 쓸 때 출력은 파일에 빈 줄이 많아 보이지 않습니다. 문제는 사람이 편집해야하기 때문에 더 나은 결과물을 얻고 싶습니다.boost :: property_tree XML 예쁘 인쇄

#include <boost/property_tree/ptree.hpp> 
#include <boost/property_tree/xml_parser.hpp> 

int main(void) 
{ 
    using boost::property_tree::ptree; 
    ptree pt; 

    // reading file.xml 
    read_xml("file.xml", pt); 

    // writing the unchanged ptree in file2.xml 
    boost::property_tree::xml_writer_settings<char> settings('\t', 1); 
    write_xml("file2.xml", pt, std::locale(), settings); 

    return 0; 
} 

file.xml이 포함되어 있습니다 : 예를 들어

나는 작은 테스트 프로그램 작성 프로그램 file2.xml을 실행 한 후

<?xml version="1.0" ?> 
<config> 
    <net> 
     <listenPort>10420</listenPort> 
    </net> 
</config> 

이 포함

<?xml version="1.0" encoding="utf-8"?> 
<config> 



    <net> 



     <listenPort>10420</listenPort> 
    </net> 
</config> 

인가 거기에 출력을 수동으로 가서 빈 라인을 삭제하는 것보다 더 나은 출력을 가질 수있는 방법이 있습니까?

+2

부스트 :: property_tree는 RapidXML, http://rapidxml.sourceforge.net/라는 XML 파서를 사용합니다. boost :: property_tree와 RapidXML은 모두 Marcin Kalicinski가 관리합니다. 직접 그에게 연락하는 것이 좋습니다. RapidXML 홈 페이지에서 그의 메일 주소를 찾을 수 있습니다. – user763305

+0

ildjarn에게 감사의 말을 전합니다.하지만 빈 줄은 이유가 있습니다!Btw 질문은 관리자에게 물어 보면 – foke

답변

39

이 솔루션은 read_xml로 통화에 trim_whitespace 플래그를 추가했다 :

#include <boost/property_tree/ptree.hpp> 
#include <boost/property_tree/xml_parser.hpp> 

int main(void) 
{ 
    // Create an empty property tree object 
    using boost::property_tree::ptree; 
    ptree pt; 

    // reading file.xml 
    read_xml("file.xml", pt, boost::property_tree::xml_parser::trim_whitespace); 

    // writing the unchanged ptree in file2.xml 
    boost::property_tree::xml_writer_settings<char> settings('\t', 1); 
    write_xml("file2.xml", pt, std::locale(), settings); 

    return 0; 
} 

플래그는 here 설명되어 있지만, 라이브러리 (세바스티앙 REDL)의 현재 메인테이너가 응답하고 날 지점 친절 충분했다 그것.

+1

경고가 표시됩니다. 경고 : trim_whitespace는 XML의 공백뿐만 아니라 다른 요소를 포함하지 않는 요소의 공백도 제거합니다 : ' xx'읽습니다. 마치' xx'입니다. –

+7

이것을 얻으려면 * read * 설정을 변경해야합니다 (특별히 @AndreasHaferburg 주석 뒤에). 어쨌든 Boost의 현재 버전에서는'xml_writer_settings '('char'이 아님)을 사용해야합니다. – alfC

+0

"여기"링크가 업데이트되었습니다 : http://www.boost.org/doc/libs/1_58_0/doc/html/boost/property_tree/xml_parser/read_xml_idp82929296.html – alfC

3

이 질문은 아주 오래된,하지만 많이 악화됐다 때문에 나는 지금 property_tree, 내 의견이 버그에서

에 개행 문자 변환, 최근에 다시 문제를 조사 요소 때문에 어떤 공백만을 포함합니다 - 개행, 공백 및 탭은 텍스트 요소로 취급됩니다. trim_whitespace는 bandaid이며 property_tree의 모든 공백을 표준화합니다.

나는 여기에 버그를보고도 사용하지 않을 경우 trim_whitespace에 부스트 1.59에서이 문제를 해결하기 위해 .diff를 첨부 :

boost::property_tree::xml_writer_settings<char> settings('\t', 1); 

부스트 컴파일 : 그 노력에 대한 https://svn.boost.org/trac/boost/ticket/11600

2

-1.60.0으로 VisualStudio 2013 년 당신이 얻을 수

vmtknetworktest.cpp(259) : see reference to class template instantiation 'boost::property_tree::xml_parser::xml_writer_settings<char>' being compiled 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(38): error C2039: 'value_type' : is not a member of '`global namespace'' 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(38): error C2146: syntax error : missing ';' before identifier 'Ch' 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(38): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(40): error C2061: syntax error : identifier 'Ch' 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(49): error C2146: syntax error : missing ';' before identifier 'indent_char' 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C2825: 'Str': must be a class or namespace when followed by '::' 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C2039: 'size_type' : is not a member of '`global namespace'' 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C2146: syntax error : missing ';' before identifier 'indent_count' 
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
vmtknetworktest.cpp(259): error C2661: 'boost::property_tree::xml_parser::xml_writer_settings<char>::xml_writer_settings' : no overloaded function takes 3 arguments 

을 그리고 여기까지 :

작업에 발견 할 수 6,

https://svn.boost.org/trac/boost/ticket/10272

솔루션은 템플릿에 표준 : : 문자열을 사용하는 것입니다.

pt::write_xml(file_name, params, std::locale(), pt::xml_writer_make_settings<std::string>(' ', 4)); 

여기에 설명 :

https://stackoverflow.com/a/35043551/7170333

관련 문제