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 Matthias Hopfer <Ma...@eureka.de> on 2010/02/18 08:22:22 UTC

[v2.8] DOMNode::hasChildNodes(...) & DOMNode::removeChild(...)

Hi there,

The following structure 
//
<root>	
	<evt>
		<pos a="test" b="test" />
		<pos a="test" b="test" />
		<pos a="test" b="test" />
	</evt>
</root>
//
should be transformed into
//
<root>	
	<evt>
		<pos a="test" b="test" />
	</evt>
	<evt>
		<pos a="test" b="test" />
	</evt>
	<evt>
		<pos a="test" b="test" />
	</evt>
</root>
//
Initiating and parsing the xml file assumed to be working properly.
The following code snippets are applied:

// step 1: altering document
DOMDocument* doc = m_pDOMparser->getDocument();
for ( unsigned z = 1; z < m_p_evtList->getLength(); ++z )
{
    if ( doc != NULL )
    {
        if ( m_p_evtList->item ( z ) != NULL )
        {
            DOMElement* ele = doc->createElement ( CUnicodeConv (
strXML_UNI_EVENT.c_str() ).getXMLCh() );
            ele->appendChild ( m_p_evtList->item ( z ) );
            m_p_root->appendChild ( ele );
        }
    }
}

// 2nd step: removing empty elements
DOMNodeList* rootChildren = m_p_root->getChildNodes();
for ( unsigned u = 0; u < rootChildren->getLength(); ++u )
{
    DOMNode* node = rootChildren->item ( u );
    if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
    {
        if ( !node->hasChildNodes() )
        {
            DOMNode* removed = m_p_root->removeChild ( node );
            removed->release();
        }
    }
}

After 'step 1' the document looks like this:
//
<root>
	<evt>
	</evt>	
	<evt>
		<pos a="test" b="test" />
	</evt>
	<evt>
		<pos a="test" b="test" />
	</evt>
	<evt>
		<pos a="test" b="test" />
	</evt>
</root>
//

So i tried to removed to removed the redundant element with 'step 2'.

The problem:
'node->hasChildNodes()' always returns 'true' and therefore no element
will be removed.

Any hints?

-mh

[SOLVED]Re: [v2.8] DOMNode::hasChildNodes(...) & amp; DOMNode::removeChild(...)

Posted by matthias hoepfer <ma...@eureka.de>.
// removing redundant nodes with no elements
DOMNodeList* unitChildren = m_p_root->getChildNodes();
for ( unsigned u = 1; u < unitChildren->getLength(); ++u )
{
    DOMNode* node = unitChildren->item ( u );
    if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
    {
        bool validChild = false;
        DOMElement* childElement = reinterpret_cast<DOMElement*>( node );
        DOMNode* childNode = childElement->getFirstChild();
        while ( childNode )
        {
            if ( childNode->getNodeType() == DOMNode::ELEMENT_NODE )
            {
                // child has valid element children
                validChild = true;
            }
            childNode = childNode->getNextSibling();
        }
        if ( !validChild )
        {
            // node has no valid children elements -> remove
            DOMNode* removed = m_p_root->removeChild ( node );
            removed->release();
        }
    }
}

Resolved. 
The key is: checking which type of node has which kind of children.

-mh