This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Memory leakage when converting Xerces DOM document to string
- From: stig dot garberg at no dot abb dot com
- To: XSL-List at lists dot mulberrytech dot com
- Date: Tue, 26 Feb 2002 07:25:23 +0100
- Subject: [xsl] Memory leakage when converting Xerces DOM document to string
- Reply-to: xsl-list at lists dot mulberrytech dot com
I am implementing XML/XSL web pages for a process control system. My XML file
is built by means of Xerces DOM document (version 1.5.1). Before sending the
XML information to the Web Browser over TCP/IP, I convert the DOM document to a
STL string. This convertion, which is build by means of a recursive function
(see below), works correct for about 10 minutes when the same Web page is
requested each tenth second, but then a memory leakage occurs. Afterwards, for
each new refresh of the Web page, more memory is lost until the PC crashes.
My quistions are :
Have I done something wrong in my implementation of the convertion of DOM
document to STL string (see conversion function below)?
Is there a built-in convertion from DOM document to STL string or char* in
Xerces ?
Does it exist other ways of transmitting the DOM document to the Web Browser
over TCP/IP than converting the DOM document to STL string or char* ?
/Stig Garberg
void FetchXmlStr(DOM_Node &toWrite,string &str)
// Convert DOM_Node to string
//
// Convert DOM node, and, recursively, all of its children to a text string.
This
// function is the heart of writing a DOM tree out as XML source. Give it
// a document node and it will do the whole thing.
{
// Get the name and value out for convenience
DOMString nodeName = toWrite.getNodeName();
DOMString nodeValue = toWrite.getNodeValue();
unsigned long lent = nodeValue.length();
switch (toWrite.getNodeType())
{
case DOM_Node::TEXT_NODE:
{
string szNodeName(nodeValue.transcode());
str += szNodeName;
break;
}
case DOM_Node::PROCESSING_INSTRUCTION_NODE :
{
str.append("<?");
string szNodeName(nodeName.transcode());
str += szNodeName;
if (lent > 0)
{
str.append(" ");
string szNodeValue(nodeValue.transcode());
str += szNodeValue;
}
str.append("?>");
break;
}
case DOM_Node::DOCUMENT_NODE :
{
DOM_Node child = toWrite.getFirstChild();
while( child != 0)
{
FetchXmlStr(child, str);
str.append("\n");
child = child.getNextSibling();
}
break;
}
case DOM_Node::ELEMENT_NODE :
{
str.append("<");
string szNodeName(nodeName.transcode());
str += szNodeName;
// Output any attributes on this element
DOM_NamedNodeMap attributes = toWrite.getAttributes();
int attrCount = attributes.getLength();
for (int i = 0; i < attrCount; i++)
{
DOM_Node attribute = attributes.item(i);
str.append(" ");
string szAttributeName(attribute.getNodeName().transcode());
str += szAttributeName;
str.append("=\"");
string szAttributeValue(attribute.getNodeValue().transcode());
str += szAttributeValue;
str.append("\"");
}
DOM_Node child = toWrite.getFirstChild();
if (child != 0)
{
str.append(">");
while( child != 0)
{
FetchXmlStr(child, str);
child = child.getNextSibling();
}
str.append("</");
str += szNodeName;
str.append(">");
}
else
{
str.append("/");
str.append(">");
}
break;
}
case DOM_Node::ENTITY_REFERENCE_NODE:
{
DOM_Node child;
str.append("&");
string szNodeName(nodeValue.transcode());
str += szNodeName;
str.append(";");
break;
}
case DOM_Node::CDATA_SECTION_NODE:
{
str.append("<![CDATA[");
string szNodeValue(nodeValue.transcode());
str += szNodeValue;
str.append("<]]>");
break;
}
case DOM_Node::COMMENT_NODE:
{
str.append("<!--");
string szNodeValue(nodeValue.transcode());
str += szNodeValue;
str.append("-->");
break;
}
case DOM_Node::DOCUMENT_TYPE_NODE:
{
DOM_DocumentType doctype = (DOM_DocumentType &)toWrite;;
str.append("<!DOCTYPE ");
string szNodeName(nodeName.transcode());
str += szNodeName;
DOMString id = doctype.getPublicId();
if (id != 0)
{
str.append(" PUBLIC \"");
string szId(id.transcode());
str += szId;
str.append("\"");
id = doctype.getSystemId();
if (id != 0)
{
str.append(" \"");
string szId(id.transcode());
str += szId;
str.append("\"");
}
}
else
{
id = doctype.getSystemId();
if (id != 0)
{
str.append(" SYSTEM \"");
string szId(id.transcode());
str += szId;
str.append("\"");
}
}
id = doctype.getInternalSubset();
if (id !=0)
{
str.append("[");
string szId(id.transcode());
str += szId;
str.append("]");
}
str.append(">");
break;
}
case DOM_Node::ENTITY_NODE:
{
str.append("<!ENTITY ");
string szNodeName(nodeName.transcode());
str += szNodeName;
DOMString id = ((DOM_Entity &)toWrite).getPublicId();
if (id != 0)
str.append("PUBLIC \"");
string szId(id.transcode());
str += szId;
str.append("\"");
id = ((DOM_Entity &)toWrite).getSystemId();
if (id != 0)
{
str.append("SYSTEM \"");
string szId(id.transcode());
str += szId;
str.append("\"");
}
id = ((DOM_Entity &)toWrite).getNotationName();
if (id != 0)
{
str.append("NDATA \"");
string szId(id.transcode());
str += szId;
str.append("\"");
}
str.append("\r\n");
break;
}
case DOM_Node::XML_DECL_NODE:
{
DOMString DomStr;
str.append("<?xml version=\"");
str += (((DOM_XMLDecl &)toWrite).getVersion()).transcode();
str.append("\"");
str.append(" encoding=\"ISO-8859-1\"");
str += gEncodingName;
DomStr = ((DOM_XMLDecl &)toWrite).getStandalone();
if (DomStr != 0)
{
str.append("\" standalone=\"");
string szDomStr(DomStr.transcode());
str += szDomStr;
}
str.append("?>\r");
break;
}
default:
{
char trbuf[100];
printf(trbuf,"Unrecognized node type =
%d",(long)toWrite.getNodeType());
}
}
}
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list