You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by David N Bertoni/CAM/Lotus <Da...@lotus.com> on 2000/03/02 03:44:37 UTC

Re: XML4C DOM cloneNode and casting question

Assigning tmp_node to element is fine here, since the assignment operator
does the right thing with the reference counts.  The DOM_* classes are
"smart-pointers", so you can think of them as pointers -- in this context,
tmp_node is an alias for a node in the document.  The reference-counting in
the implementation ensures that you cannot have a dangling reference to a
node in the document.  (By the way, there is no copy-construction going on
here that I can see.)

Can you be more explicit about what you mean by "gets pretty miserable"?
Casting using (DOM_Element&) should be fine.  As far as I know, C-style
casts are allowed to do just about anything.  Maybe you should try a
new-style cast:

   element = static_cast<DOM_Element&>(tmp_node);

You might also need to include DOM_Element.hpp, so the compiler understands
that DOM_Element derives from DOM_Node.

Dave





"KRAUSE,MIKE (HP-FtCollins,ex1)" <mi...@hp.com> on 03/02/2000
08:05:58 PM

Please respond to xerces-dev@xml.apache.org

To:   xerces-dev@xml.apache.org
cc:    (bcc: David N Bertoni/CAM/Lotus)

Subject:  XML4C DOM cloneNode and casting question


Hello,

Given the following C++ function (which is supposed to return the first
child element of start matching the name element_name):

1:  void FindFirstElement(const DOM_Node &start, const DOMString
&element_name, DOM_Element &element) {
2:    bool bfound = false;
3:    DOM_Node tmp_node = start;
4:
5:    if (start != 0) { // Make sure we were passed a valid parent node
6:       tmp_node = start.getFirstChild();
7:
8:       // Now look for first-level children of the passed in parent node
searching
9:       // for the first one whose node name matches element_name
10:
11:      while (!bfound && (tmp_node != 0)) {
12:         if (tmp_node.getNodeType() == DOM_Node::ELEMENT_NODE) {
13:            if (tmp_node.getNodeName().equals(element_name)) {
14:               bfound = true;
15:               element = tmp_node; // This is the line in question
16:            }
17:         }
18:
19:         if (!bfound) {
20:            tmp_node = tmp_node.getNextSibling();
21:         }
22:      }
23:   }
24: }

Can you tell me

a) Should I be doing a shallow cloneNode on line 15 since tmp_node is only
scoped by this function or will the copy constructor for DOM_Node handle
this?

and...

b) What should be the correct way of down casting a DOM_Node to a
DOM_Element?  I have run into this problem on several occasions where I get
a DOM_Node by using the DOM traversal functions and, after determining its
type to be DOM_Node::ELEMENT_NODE, I would like to start using it as a
DOM_Element node.  Casting works on the Sun and MS compilers here (e.g.,
replacing line 15 with element = (DOM_Element &)tmp_node;  But the HP-UX
compiler we're using gets pretty miserable about this cast and returns an
error.  I've tried using the dynamic_cast technique, but to no avail on
HP-UX.

Has anyone dealt with these issues and have any ideas?

Regards,

Mike Krause
Hewlett-Packard Company