在Windows上使用Xerces 3.0.1和C ++编写XML [英] writing XML with Xerces 3.0.1 and C++ on windows
本文介绍了在Windows上使用Xerces 3.0.1和C ++编写XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XMLFormatTarget * formatTarget = new LocalFileFormatTarget( TARGETPATH);有人可以解释为什么我的代码适用于相对路径,但不是绝对路径吗?
非常感谢。
const int ABSOLUTE_PATH_FILENAME_PREFIX_SIZE = 9;
void OutputXML(xercesc :: DOMDocument * pmyDOMDocument,std :: string filePath)
{
//返回具有所需功能的第一个注册的实现。在这种情况下,我们在具有LS功能...或加载/保存的DOM实现之后。
DOMImplementation * implementation = DOMImplementationRegistry :: getDOMImplementation(LLS);
//创建一个DOMLSSerializer,用于将DOM树序列化为XML文档。
DOMLSSerializer * serializer =((DOMImplementationLS *)) - > createLSSerializer();
//通过插入换行符使输出更加人性化。
if(serializer-> getDomConfig() - > canSetParameter(XMLUni :: fgDOMWRTFormatPrettyPrint,true))
serializer-> getDomConfig() - > setParameter(XMLUni :: fgDOMWRTFormatPrettyPrint,true);
//要编写的XML中要使用的行尾序列。
serializer-> setNewLine(XMLString :: transcode(\r\\\
));
//将路径转换为Xerces兼容的XMLCh *。
XMLCh * tempFilePath = XMLString :: transcode(filePath.c_str());
//计算字符串的长度。
const int pathLen = XMLString :: stringLen(tempFilePath);
//为Xerces字符串分配内存以充分保存路径。
XMLCh * targetPath =(XMLCh *)XMLPlatformUtils :: fgMemoryManager-> allocate((pathLen + ABSOLUTE_PATH_FILENAME_PREFIX_SIZE)* sizeof(XMLCh));
//将依赖平台的绝对路径文件修复为标准URI格式。
XMLString :: fixURI(tempFilePath,targetPath);
//指定XML输出的目标。
XMLFormatTarget * formatTarget = new LocalFileFormatTarget(targetPath);
// XMLFormatTarget * myFormTarget = new StdOutFormatTarget();
//创建一个新的空输出目标对象。
DOMLSOutput * output =((DOMImplementationLS *)实现) - > createLSOutput();
//将流设置为我们的目标。
output-> setByteStream(formatTarget);
//将序列化输出写入目的地。
serializer-> write(pmyDOMDocument,output);
//清理。
serializer-> release();
XMLString :: release(& tempFilePath);
删除formatTarget;
output-> release();
}
解决方案
您是否使用Windows Vista?也许你没有必要的权限?
看到这个问题:两行Xerces程序中的异常
i have the following function i wrote to create an XML file using Xerces 3.0.1, if i call this function with a filePath of "foo.xml" or "../foo.xml" it works great, but if i pass in "c:/foo.xml" then i get an exception on this line
XMLFormatTarget *formatTarget = new LocalFileFormatTarget(targetPath);
can someone explain why my code works for relative paths, but not absolute paths please? many thanks.
const int ABSOLUTE_PATH_FILENAME_PREFIX_SIZE = 9;
void OutputXML(xercesc::DOMDocument* pmyDOMDocument, std::string filePath)
{
//Return the first registered implementation that has the desired features. In this case, we are after a DOM implementation that has the LS feature... or Load/Save.
DOMImplementation *implementation = DOMImplementationRegistry::getDOMImplementation(L"LS");
// Create a DOMLSSerializer which is used to serialize a DOM tree into an XML document.
DOMLSSerializer *serializer = ((DOMImplementationLS*)implementation)->createLSSerializer();
// Make the output more human readable by inserting line feeds.
if (serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
// The end-of-line sequence of characters to be used in the XML being written out.
serializer->setNewLine(XMLString::transcode("\r\n"));
// Convert the path into Xerces compatible XMLCh*.
XMLCh *tempFilePath = XMLString::transcode(filePath.c_str());
// Calculate the length of the string.
const int pathLen = XMLString::stringLen(tempFilePath);
// Allocate memory for a Xerces string sufficent to hold the path.
XMLCh *targetPath = (XMLCh*)XMLPlatformUtils::fgMemoryManager->allocate((pathLen + ABSOLUTE_PATH_FILENAME_PREFIX_SIZE) * sizeof(XMLCh));
// Fixes a platform dependent absolute path filename to standard URI form.
XMLString::fixURI(tempFilePath, targetPath);
// Specify the target for the XML output.
XMLFormatTarget *formatTarget = new LocalFileFormatTarget(targetPath);
//XMLFormatTarget *myFormTarget = new StdOutFormatTarget();
// Create a new empty output destination object.
DOMLSOutput *output = ((DOMImplementationLS*)implementation)->createLSOutput();
// Set the stream to our target.
output->setByteStream(formatTarget);
// Write the serialized output to the destination.
serializer->write(pmyDOMDocument, output);
// Cleanup.
serializer->release();
XMLString::release(&tempFilePath);
delete formatTarget;
output->release();
}
解决方案
Are you using Windows Vista? perhaps you don't have the necessary permissions? See this question: Exception in two line Xerces program
这篇关于在Windows上使用Xerces 3.0.1和C ++编写XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文