0

The following code gives me an exception on the XMLFormatTarget line, but if I change the string from "C:/test.xml" to "test.xml" it works fine.

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>

using namespace xercesc;

int main()
{
    XMLPlatformUtils::Initialize();

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

    return 0;
}

[edit] Xerces exception is:

Error Message: unable to open file 'C:\test.xml'

Windows exception is:

Access is denied

4

3 回答 3

1

尝试转码文件名:

// 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);

根据这个类似问题的回答。

于 2010-06-22T10:27:00.897 回答
1

可能是您没有足够的权限写入C:\. 在这种情况下,Xerces 可能会报告引发异常的错误。

Access Denied如果您尝试在没有管理员凭据的情况下写入系统目录,通常会出现异常。


也许它也与目录分隔符有关:

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

在 Windows 上,目录分隔符是反斜杠“\”。一些图书馆不在乎(而且我从未使用过 Xerces,所以我不知道)。在CandC++中,反斜杠也是一个转义字符,所以如果你想在你的字符串中添加一个文字“\”,你必须加倍它。

此外,告诉我们您遇到的例外情况对我们有更大的帮助。


没有直接关系,但从您的代码来看,您似乎从来没有delete formatTarget. 我假设这是示例代码,但如果不是,您应该将以下行添加到您的代码中:

delete formatTarget;

或者改用作用域指针

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

避免内存泄漏。

于 2010-06-22T08:35:34.940 回答
0

如果仅使用test.xml,则指定相对于当前工作目录的路径(通常是程序的启动位置)。因此,如果您的程序不直接位于 C: 驱动器上,则两次运行可能指向不同的文件。C:\test.xml可能有错误,但C:\Path\to\your\program\test.xml正确,因此后者也不例外。

无论如何,正如 ereOn 所说,如果我们知道抛出了哪个异常,那将会有所帮助。

于 2010-06-22T08:46:50.920 回答