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 David Hoffer <da...@iserv.net> on 2003/09/15 00:06:07 UTC

DOM performance/coding issue

I have a question about how to effectively parser a large DOM document.  For
example, I have a lot of 'PatchData' elements.  I am looking for the set of
these where the child element 'CbName' matches a certain string.  The
following code takes 30 seconds just to loop through the DOMNodeList before
it finds the first matching child.  How can I make this faster?

DOMNodeList* pDOMNodeList =
m_pDocument->getDocumentElement()->getElementsByTagNameNS(NULL,
L"PatchData");

for (int i=0; i<pDOMNodeList->getLength(); i++)
{
	DOMNode* pDOMNode = pDOMNodeList->item(i);
	DOMNodeList* pDOMNodeList = ((const
DOMElement*)pDOMNode)->getElementsByTagNameNS(NULL, L"CbName");
	pDOMNode = pDOMNodeList->item(0);
	DOMNode* pChildNode = pDOMNode->getFirstChild();
	std::wstring wstrTagValue = pChildNode->getNodeValue();

	if (wcscmp(wstrTagValue.c_str(), m_wstrColorbarName.c_str()) == 0)
	{
		// This is one of the ones I want...I spend very little time here.
	}

	// I spend 30 seconds here, in a loop...
}

My XML structure is like...
<PatchData>
    <CbName>abc</CbName>
    <Location>0</Location>
    <Type>0</Type>
    <Width>5.15</Width>
    <Enabled>true</Enabled>
</PatchData>
<PatchData>
    <CbName>abc</CbName>
    <Location>1</Location>
    <Type>1</Type>
    <Width>5.25</Width>
    <Enabled>false</Enabled>
</PatchData>

-dh


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: DOM performance/coding issue

Posted by Alberto Massari <am...@progress.com>.
At 18.06 14/09/2003 -0400, you wrote:
>I have a question about how to effectively parser a large DOM document.  For
>example, I have a lot of 'PatchData' elements.  I am looking for the set of
>these where the child element 'CbName' matches a certain string.  The
>following code takes 30 seconds just to loop through the DOMNodeList before
>it finds the first matching child.  How can I make this faster?

If you just need the first matching node, don't use getElementsByTagNameNS, 
but directly iterate over the child node; that's because when you call 
getElementsByTagNameNS you are going to iterate over all the child nodes, 
and build a list containing all the "PatchData" elements.

Alberto



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: DOM performance/coding issue

Posted by igbert <gi...@imageguide.us>.
I attack this problem by doing incremental parsing such 
as what the Sax examples do.

You are going to get a very large tree if you parse first,
then process.  It seems to me that that may be what is
taking so long.

YMMV:)

Bert Williams.

-----Original Message-----
From: David Hoffer [mailto:dahoffer@iserv.net] 
Sent: Sunday, September 14, 2003 6:06 PM
To: xerces-c-dev@xml.apache.org
Subject: DOM performance/coding issue

I have a question about how to effectively parser a large DOM document.
For
example, I have a lot of 'PatchData' elements.  I am looking for the set
of
these where the child element 'CbName' matches a certain string.  The
following code takes 30 seconds just to loop through the DOMNodeList
before
it finds the first matching child.  How can I make this faster?

DOMNodeList* pDOMNodeList =
m_pDocument->getDocumentElement()->getElementsByTagNameNS(NULL,
L"PatchData");

for (int i=0; i<pDOMNodeList->getLength(); i++)
{
	DOMNode* pDOMNode = pDOMNodeList->item(i);
	DOMNodeList* pDOMNodeList = ((const
DOMElement*)pDOMNode)->getElementsByTagNameNS(NULL, L"CbName");
	pDOMNode = pDOMNodeList->item(0);
	DOMNode* pChildNode = pDOMNode->getFirstChild();
	std::wstring wstrTagValue = pChildNode->getNodeValue();

	if (wcscmp(wstrTagValue.c_str(), m_wstrColorbarName.c_str()) ==
0)
	{
		// This is one of the ones I want...I spend very little
time here.
	}

	// I spend 30 seconds here, in a loop...
}

My XML structure is like...
<PatchData>
    <CbName>abc</CbName>
    <Location>0</Location>
    <Type>0</Type>
    <Width>5.15</Width>
    <Enabled>true</Enabled>
</PatchData>
<PatchData>
    <CbName>abc</CbName>
    <Location>1</Location>
    <Type>1</Type>
    <Width>5.25</Width>
    <Enabled>false</Enabled>
</PatchData>

-dh


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: DOM performance/coding issue

Posted by Dean Roddey <dr...@charmedquark.com>.
If you aren't going to actually need the tree structure, use the SAX
interface instead and just pull the elements out as you see them, and store
the ones you want in your own data structures. It'll probably be far faster.

-------------------------------------
Dean Roddey
The Charmed Quark Controller
droddey@charmedquark.com
www.charmedquark.com
 


-----Original Message-----
From: David Hoffer [mailto:dahoffer@iserv.net] 
Sent: Sunday, September 14, 2003 3:06 PM
To: xerces-c-dev@xml.apache.org
Subject: DOM performance/coding issue


I have a question about how to effectively parser a large DOM document.  For
example, I have a lot of 'PatchData' elements.  I am looking for the set of
these where the child element 'CbName' matches a certain string.  The
following code takes 30 seconds just to loop through the DOMNodeList before
it finds the first matching child.  How can I make this faster?

DOMNodeList* pDOMNodeList =
m_pDocument->getDocumentElement()->getElementsByTagNameNS(NULL,
L"PatchData");

for (int i=0; i<pDOMNodeList->getLength(); i++)
{
	DOMNode* pDOMNode = pDOMNodeList->item(i);
	DOMNodeList* pDOMNodeList = ((const
DOMElement*)pDOMNode)->getElementsByTagNameNS(NULL, L"CbName");
	pDOMNode = pDOMNodeList->item(0);
	DOMNode* pChildNode = pDOMNode->getFirstChild();
	std::wstring wstrTagValue = pChildNode->getNodeValue();

	if (wcscmp(wstrTagValue.c_str(), m_wstrColorbarName.c_str()) == 0)
	{
		// This is one of the ones I want...I spend very little time
here.
	}

	// I spend 30 seconds here, in a loop...
}

My XML structure is like...
<PatchData>
    <CbName>abc</CbName>
    <Location>0</Location>
    <Type>0</Type>
    <Width>5.15</Width>
    <Enabled>true</Enabled>
</PatchData>
<PatchData>
    <CbName>abc</CbName>
    <Location>1</Location>
    <Type>1</Type>
    <Width>5.25</Width>
    <Enabled>false</Enabled>
</PatchData>

-dh


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org