You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by "Foong, Tzeweng" <Tz...@team.telstra.com> on 2000/12/11 01:03:55 UTC

another newbie Casting question.

Hello all,

First let me say that this is  agreat little mailing list.. !!  ita a shame
we 
dont have a more cmprehensive programming FAQ yet.  But the 
answer have been good.

I have been programming using the DOM interface .  I chose it as I needed 
to read and compare several XML documents, sort of using them like a 
database, and also write out another XML file.  The infamous DOMprint 
sample has been a great help and so has the CreateDomDocument example.

However I am not too clear on one or 2 things that I hope someone 
can help me clear up.

Firstly, we create a parser object. then we use the parse method to read 
in an xml document. then we use the getDocument() method to get the 
DOM_Document.  We then have several methods of the DOM_Document 
object that togather return DOM_Node objects..   Now the question is...
These DOM Node Objects are dumb DOM_Node object. Ie the node may be 
a DOM_Element which has DOM_Attr nodes atached to it.  How do I use a 
DOM_Node as a DOM_Element which has several nice methods that return 
Attributes etc....

A related question is if the DOM_Node is NOT DOM_Element but I want to 
use it as one? How do I do it .. is it the same as the solution to the 
question above what ever the answer may be?
(actually I want To use the DOM_Node (a DOM_Element) like a DOM_Document 
ie I want to call the method getElementsByTagName() which is in the 
DOM_Document but not in the DOM_Element). 

A a side issue.. How do you return a DOM_Node from a function?
I keep getting empty Nodes when I try to return a DOM_Node.
The oonly way I have been able to return a DOM Node is 
to pass it in as and argumnet
eg:

void findChildNodeNamed(char *aNodeName, DOM_Node &aNode, DOM_Node &aChild)
{
    aChild = aNode.getFirstChild();
    while (!aChild.isNull() && !aChild.getNodeName().equals(aNodeName) )
    {   
        aChild = aChild.getNextSibling();
    }
}

It would be much nicer to return the Chile node and have a function like
something like this.

DOM_Node &findChildNodeNamed(char *aNodeName, DOM_Node &aNode)
or
DOM_Node *findChildNodeNamed(char *aNodeName, DOM_Node &aNode)

But I canot get either to return any thing sensible.. I am sure its a simple
thing I am not seeing...

;-) BTW you can use my code above...It works fine...

Tze Weng Foong


Re: another newbie Casting question.

Posted by Dean Roddey <dr...@charmedquark.com>.
The answer is in the same DOMPrint example AFAIK. You check the node type,
via the base node interface. If its a type you want, then you cast it,
something like this:

DOM_Element& elem = (DOM_Element&)node;

assuming that you know that the node is really an element. If its any of the
other types, do something similar.

When returning nodes, return them by value. They are really just smart
pointers, so there isn't much being actually copied when this is done. But
insures that the correct reference counting inside the smart pointer is
done.

--------------------------
Dean Roddey
The CIDLib C++ Frameworks
Charmed Quark Software
droddey@charmedquark.com
http://www.charmedquark.com

"It takes two buttocks to make friction"
    - African Proverb


----- Original Message -----
From: "Foong, Tzeweng" <Tz...@team.telstra.com>
To: <xe...@xml.apache.org>
Sent: Sunday, December 10, 2000 4:03 PM
Subject: another newbie Casting question.