You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by T MacAdam <ns...@yahoo.com> on 2004/07/28 04:02:52 UTC
Blank lines introduced before CDATA sections on DOM write
Hi,
I'm a bit of a newbie to Xerces (and XML for that
matter), but I've searched high and low for an answer
to my problem: basically, I've downloaded the latest
(2.5.0) version for Windows and I'm noticing that
Xerces is inserting blank lines into my XML file just
above each CDATA section on output to file (using a
DOMWriter). For instance (a trivial example), the
following file:
<script>
<![CDATA[Some Text]]>
</script>
becomes:
<?xml version="1.0" encoding="UTF-8" standalone="no"
?>
<script>
<![CDATA[Some Text]]>
</script>
when simply read in using a DOMBuilder and immediately
written out using a DOMWriter (if the above doesn't
show it, I am seeing 3 blank lines above the CDATA
tag). I do nothing except read the file in and write
it out using "pretty printing". What's worse, if I
take this new file and again read/write it, then there
are even more blank lines (i.e. they accumulate).
Does anyone know why this is? Do I have empty text
nodes or something? The funny thing is, this happens
with version 2.5.0, but NOT with 2.3.0., which I had
been previously using.
Any help would be greatly appreciated.
P.S. here is the code I'm using to read/write the
file. I believe it is as simple as it gets ;)
#include <iostream>
#include "xercesc/dom/DOM.hpp"
#include "xercesc/util/PlatformUtils.hpp"
#include "xercesc/parsers/AbstractDOMParser.hpp"
#include "xercesc/framework/LocalFileFormatTarget.hpp"
XERCES_CPP_NAMESPACE_USE
int main(int argc, char* argv[])
{
if( argc < 3 ) {
std::cout << "Usage: TextXercesIO infile.xml
outfile.xml\n";
std::cout.flush();
return 1;
}
DOMDocument *m_pDOMDoc = NULL;
DOMWriter *m_pDOMWriter = NULL;
DOMBuilder *m_pDOMBuilder = NULL;
// initialize the environment:
try {
XMLPlatformUtils::Initialize();
}
catch( /*const XMLException& toCatch*/ ... ) {
return 1;
}
static const XMLCh gLS[] = { chLatin_L, chLatin_S,
chNull };
DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(gLS);
m_pDOMBuilder =
((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS,
0);
m_pDOMWriter =
((DOMImplementationLS*)impl)->createDOMWriter();
// set the builder so that when queried for the
parsed document, it hands over
// ownership of the document to this object:
if(
m_pDOMBuilder->canSetFeature(XMLUni::fgXercesUserAdoptsDOMDocument,
true) )
m_pDOMBuilder->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument,
true);
else {
return 1;
}
// pretty print:
if(
m_pDOMWriter->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true) )
m_pDOMWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
// read the file in:
const char* xmlFileIn = argv[1];
try
{
m_pDOMBuilder->resetDocumentPool();
m_pDOMDoc =
m_pDOMBuilder->parseURI(xmlFileIn);
}
catch(...)
{
std::cout << "An error occurred reading.\n";
std::cout.flush();
return 1;
}
// write the file out:
const char* xmlFileOut = argv[2];
XMLFormatTarget *myFormTarget = new
LocalFileFormatTarget(xmlFileOut);
try
{
m_pDOMWriter->writeNode(myFormTarget,
*m_pDOMDoc);
}
catch(...)
{
std::cout << "An error occurred writing.\n";
std::cout.flush();
return 1;
}
delete myFormTarget;
// terminate environment and delete doc if it
still exists:
if( m_pDOMDoc )
m_pDOMDoc->release();
m_pDOMBuilder->release();
delete m_pDOMWriter;
XMLPlatformUtils::Terminate();
m_pDOMBuilder = NULL;
m_pDOMWriter = NULL;
m_pDOMDoc = NULL;
return 0;
}
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org