Benoit Lefevre wrote:
> Hi
>
> I'm having troubles to use the DOM part of Xerces C++.
> On one hand i have a DOM object that i've loaded from a XML file.
> On the other hand i have my own acces structure (a map).
>
> My problem is to be able to fill my structure according to the DOM
object.
> Actualy my map is a <string,DOM_Element *> map.
> I'd like to use it to have a quick access to some part of the DOM
object.
> when i parse the DOM object to fill my map i use this kind of code :
>
> void recursiveFill(DOM_Element &theDom)
> {
>
> ...
> myMap_.insert( pair<string,DOM_Element *>(aString, &theDom));
> ...
>
> DOM_NODEList theList = theDom.getChildNodes();
> for( int i=0 ; i < theList.getLength() ; i++ )
> if(theList.item(i).getNodeType == DOM_Node::ELEMENT_NODE)
> recursiveFill( (DOM_Element &) theList.item(i));
> ...
> }
You're taking a non-const reference to a temporary in the call to
recursiveFill and any decent compiler should report an error. If your's
does not, you should consider using a better compiler.
> the problem is :
>
> - when i've finished to fill my map, all the pair that have been
> store inside are pointing to the same DOM_Element !?!!? (memory
speeking).
Yes, because you're taking the address of the temporary return by
DOM_NODEList::item(), and that temporary ends up at the same address on the
stack at each recursive call.
>
> - when i try to display the memory address of the "theDom" variable
during the
> recusive calls, this one never change !?!!??!
Again, you're pointing to the same temporary.
> - even if the address of "theDom" variable never change, the data
it's
> representing change (during recusive calls).
>
Yes, and you're lucky because the temporary you're pointing to has been
destroyed. Officially, the behavior is undefined in such cases.
> i guess it is due to the use of a function call by reference, but i'm
not that good
> in C++ to correct the problem.
Yes and no. You should never take the address of a temporary -- that's the
problem. The other is that you should use DOM_Node and its descendants
properly. Always pass them and store them by value, since they are
smart-pointers to the implementation classes:
map<string,DOM_Element> m_map;
void recursiveFill(DOM_Element theDom)
{
...
myMap_.insert( pair<string,DOM_Element>(aString, theDom));
...
DOM_NodeList theList = theDom.getChildNodes();
for( unsigned int i=0 ; i < theList.getLength() ; i++ )
if(theList.item(i).getNodeType() == DOM_Node::ELEMENT_NODE)
recursiveFill( (const DOM_Element&) theList.item(i));
// or better yet:
// recursiveFill(static_cast<const
DOM_Element&>(theList.item(i)));
...
}
> None the less, i've tryed to use pointers insted of references, but
except a new
> casting problem it didn't change anything
>
> If someone have an idea, a clue, an advice or even better an exemple
...
> i'm higly interessed.
> PS: sorry, i'm not fluent in english neither :).
That's OK, because I'm not fluent in French either...
> thank you.
You're welcome.
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]