在vc++中加载XML值并将其传递为字符串

load xml values and pass it into string in vc++

本文关键字:字符串 vc++ 加载 XML      更新时间:2023-10-16

我在vc++中工作并试图加载xml文件并将整个数据加载到字符串中,但我没有得到结果

 char text[700] = {""};
 TiXmlDocument doc( "'demotest.xml" );
 bool loadOkay = doc.LoadFile();
 if ( !loadOkay )
 {
    printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.n", doc.ErrorDesc() );
    system("PAUSE");
    exit( 1 );
}
    printf( "** Demo doc read from disk: ** nn" );
    printf( "** Printing via doc.Print **n" );
    //doc.Print( stdout );
    {
        printf( "** Printing via TiXmlPrinter **n" );
        TiXmlPrinter printer;
        doc.Accept( &printer );
        fprintf( stdout, "%s", printer.CStr() );
//upto this line its working fine in console. but when I convert this string am getting struck
        wsprintf(text, "%s", (char*)printer.CStr());
        AddLOG_message(text, 0, true);

    }

最后两行我应该得到xml的整个内容,包括标题、元素和值。请帮助。

我会这样做,用更少的C代码,更多的c++代码,并弃用长度魔术数700的危险字符数组:

TiXmlPrinter printer;
doc.Accept( &printer );
doc.Print(); // simpler for stdout output
std::string text = printer.CStr(); // easier, safer this way
AddLOG_message( text.c_str(), 0, true );