Exception in two line Xerces program

喜夏-厌秋 提交于 2019-12-02 07:40:37

It could be that you don't have sufficient permissions to write to C:\. In such a case, Xerces might report the error throwing an exception.

An Access Denied exception is typically what we could expect if you try to write to a system directory without administrator credentials.


Maybe it has also something to do with the directory separators:

XMLFormatTarget *formatTarget = new LocalFileFormatTarget("C:\\test.xml");

On Windows, directory separators are backslashes "\". Some libraries don't care (and I never used Xerces, so I can't tell). In C and C++, backslash is also an escape character and so you must double it if you want a litteral "\" in your string.

Also, telling us what was the exception you got would help us even more.


Not directly related, but from your code, it seems you never delete formatTarget. I assume this is sample code, but if it is not, you should add the following line to your code:

delete formatTarget;

Or use a scoped pointer instead:

boost::scoped_ptr<XMLFormatTarget> formatTarget(new LocalFileFormatTarget("C:\\test.xml"));

To avoid memory leaks.

jon-hanson

Try transcoding the filename:

// Convert the path into Xerces compatible XMLCh*. 
XMLCh *tempFilePath = XMLString::transcode(filePath.c_str()); 

// Specify the target for the XML output. 
XMLFormatTarget *formatTarget = new LocalFileFormatTarget(tempFilePath);

as per this answer to a similar question.

If you use only test.xml you specify a path relative to the current working directory (usually where the program was started from). So if your program is not directly on your C: drive, the two runs could point to different files. The C:\test.xml could have an error, but C:\Path\to\your\program\test.xml correct, so the latter gives you no exception.

Anyway, as ereOn said, it would help if we knew which exception is thrown.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!