You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Dave Cavell <pe...@googlemail.com> on 2010/05/26 12:08:47 UTC
problem with absolute paths when writing XML using Xerces 3.0.1
Hi, never used a mailing list before, so i hope i am doing it correctly. 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();
}
RE: problem with absolute paths when writing XML using Xerces 3.0.1
Posted by John Lilley <jl...@datalever.com>.
I don't know about the absolute path, but you need to delete/release resources in the opposite order that they were created.
john
-----Original Message-----
From: Dave Cavell [mailto:penguin.pingu@googlemail.com]
Sent: Wednesday, May 26, 2010 4:09 AM
To: c-users@xerces.apache.org
Subject: problem with absolute paths when writing XML using Xerces 3.0.1
Hi, never used a mailing list before, so i hope i am doing it correctly. 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();
}