Hi Dave,
yes, it compiles.. at least the Sun compiler version 5.3 I'm actually
using.
I copied the code from our project in a single sourcefile and without
all the object oriented stuff only to see how it works. Like in the
project, calling DOM_Document::createElement("") generates a core file,
the same when calling DOM_Document::createElement(name) when name
contains an empty string, it doesn't throw an exception.. I tried
filling name with a space -> name = " ", then createElement() throws an
exception.
See the function createNameValue(), the function createNameValue() will
be callend in main() in a try block.
Regards
Sandro
Code:
#include <iostream>
#include <xercesc/dom/deprecated/DOM.hpp>
using namespace XERCES_CPP_NAMESPACE;
using std::cout;
using std::endl;
DOM_Document myDocument;
DOM_Node makeChild(const char* childName, DOM_Node parentNode, bool
bAllowDuplicate)
//**********************************************************************
************
{
// Create child node if it does not exist, returns existing one
otherwise
DOM_Node child = parentNode;
if (!parentNode.isNull() && (strlen(childName) > 0))
{
child = parentNode.getFirstChild();
while (!child.isNull() &&
!child.getNodeName().equals(DOMString(childName)) &&
!bAllowDuplicate)
{
child = child.getNextSibling();
} // end while
if (child.isNull() || bAllowDuplicate)
{
child = myDocument.createElement(childName);
parentNode.appendChild(child);
} // end if (child.isNull() -> TRUE || (bAllowDuplicate -> TRUE)
} // end if
return child;
} // end makeChild()
DOM_Node makePath(const char* path, DOM_Node parentNode)
//******************************************************
{
char* copyOfPath = new char[strlen(path) + 1];
copyOfPath[strlen(path)] = '\0';
strncpy(copyOfPath, path, strlen(path));
char* pStart = (char*)copyOfPath;
char* pEnd = pStart;
bool bContinue = pEnd && *pEnd;
while (bContinue)
{
if ((*pEnd == '/') || (*pEnd == '\0')) // Found the limiter
{
if ((*pEnd == '\0')) bContinue = false;
*pEnd = '\0';
// Create child node if it does not exist, returns existing one
otherwise
parentNode = makeChild(pStart, parentNode, false);
pStart = pEnd + 1;
} // end if (*pEnd -> '/') OR (*pEnd -> '\0')
pEnd++;
} // end while (bContinue -> TRUE)
return parentNode;
} // end makePath()
DOM_Node makePath(const char* path)
//*********************************
{
DOM_Node nodeRet;
if (myDocument.isNull()) myDocument = DOM_Document::createDocument();
nodeRet = myDocument;
nodeRet = makePath(path, nodeRet);
return nodeRet;
} // end makePath()
void createNameValue(const char* name, const char* value, DOM_Node
parent)
//**********************************************************************
**
{
if (!parent.isNull() && (name != NULL) && (value != NULL))
{
cout << "CreateNameValue() : " << "name = \"" << name << "\",
value = \"" << value << "\"" << endl;
// Crashes when name is an empty string instead to throw an
exception
//DOM_Element element = myDocument.createElement(name);
// XXX check whats wrong
DOM_Element element;
if (strcmp(name, "") == 0)
{
cout << "createNameValue() : call createElement with empty
string" << endl;
element = myDocument.createElement("");
//cout << "createNameValue() : call createElement with one
space" << endl;
//element = myDocument.createElement(" ");
cout << "createNameValue() : createElement() was successfull"
<< endl;
}
else
{
cout << "createNameValue() : call createElement with name = \""
<< name << "\"" << endl;
element = myDocument.createElement(name);
}
// XXX
DOM_Text text = myDocument.createTextNode(value);
element.appendChild(text);
parent.appendChild(element);
} // end if (parent.isNull() -> FALSE) AND (name <> NULL) AND (value
<> NULL)
} // end createNameValue()
int main()
{
cout << "start" << endl;
XMLPlatformUtils::Initialize();
DOM_Node nodeProd = makePath("CMF/Trade/Product");
const char* name = "";
const char* value = "";
cout << "name = \"" << name << "\"" << endl;
cout << "value = \"" << value << "\"" << endl;
try
{
createNameValue(name, value, makePath("Identifiers", nodeProd));
}
catch(...)
{
cout << "Exception caught in createNameValue()" << endl;
}
} // end main()
-----Original Message-----
From: David Bertoni [mailto:[EMAIL PROTECTED]
Sent: Donnerstag, 29. Mai 2008 19:22
To: [email protected]
Subject: Re: Crash in DOM_Document::createElement() when name an empty
string
Piras Sandro (KSDB 121) wrote:
> Hi all
> thx for your help.
>
> I tested also when filling directly an empty string with
> DOM_Document::createElement("") -> crashes
How does this even compile? "" is not a pointer to a UTF-16 string.
Can you please provide a small, isolated program that reproduces the
crash?
Dave