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