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