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 "Arnold, Curt" <Cu...@hyprotech.com> on 2001/03/22 19:17:56 UTC

Casting DOM_Node to DOM_Element

A very common pattern (at least in my code) when using Xerces-J's DOM is to do something like:

for(DOM_Node node = element.getFirstChild();
    node != null; 
    node = node.getNextSibling()) {
    if(node.getNodeType() == DOM_Node.ELEMENT_NODE) {
        DOM_Element childElem = (DOM_Element) node;
        //
        //  do some element specific behavior


I haven't found a good way to do the equivalent of:

        DOM_Element childElem = (DOM_Element) node;

with Xerces-C.  I think you could do somehting like:

        DOM_Element childElem = *((DOM_Element*) &node);

but that seems pretty ugly and may be fatal if you didn't do the preliminary check for getNodeType() == ELEMENT_NODE.

What I was thinking was that adding a constructor for DOM_Element that took a DOM_Node& and would throw an exception if the node wasn't appropriate would allow you to keep the Java pattern.  (Of
course, you would want to do this for all the other DOM_Comment, DOM_ProcessingInstruction, et al).

Maybe this could go into the new DOM implementation?
 
      


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Casting DOM_Node to DOM_Element... still a problem.

Posted by Ryan Koss <rk...@acmepacket.com>.
Thanks Andy, that was what I was missing...

-ryan

----- Original Message -----
From: "Andy Heninger" <an...@jtcsv.com>
To: <xe...@xml.apache.org>
Sent: Monday, April 02, 2001 4:57 PM
Subject: Re: Casting DOM_Node to DOM_Element... still a problem.


> >  DOM_Element elem = static_cast<DOM_Element&>(nodeList.item(i));
>
>
> nodeList.item() is returning a DOM_Node by value.  The value
> that you are trying to cast doesn't have a usable memory
> address at that point - it's just a temporary value in the
> middle of an expression - so casting it to a reference type
> doesn't work.  This is what the "from rvalue of ..." in the
> error message means.
>
> Adding an intermediate variable should fix the problem
>
> DOM_Node t = nodList.item(i);
> DOM_Element elem = static_cast<DOM_Element &>t;
>
> Andy Heninger
> IBM, Cupertino, CA
> heninger@us.ibm.com
>
>
>
> ----- Original Message -----
> From: "Ryan Koss" <rk...@acmepacket.com>
> To: <xe...@xml.apache.org>
> Sent: Monday, April 02, 2001 12:35 PM
> Subject: Re: Casting DOM_Node to DOM_Element... still a problem.
>
>
> > What compiler and OS is this running on because I still get a the
> following
> > compiler error when I try to do the cast that has been stated in this
> > multi-threaded email...  I am running Mandrake linux 7.1 and my compiler
> is
> > GCC 2.95.3
> >
> > xmlobject.cpp:446: conversion to non-const reference type `class
> DOM_Element
> > &'
> > xmlobject.cpp:446: from rvalue of type `DOM_Node'
> > make: *** [xmlobject.o] Error 1
> >
> > Here is the code, I am cycling through a list of DOM_Nodes here...
> >
> >  DOM_Element elem = static_cast<DOM_Element&>(nodeList.item(i));
> >
> > Any help, ideas, notion, guesses would be greatly appreciated.  Thanks
> in
> > advance
> > -ryan
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Casting DOM_Node to DOM_Element... still a problem.

Posted by Andy Heninger <an...@jtcsv.com>.
>  DOM_Element elem = static_cast<DOM_Element&>(nodeList.item(i));


nodeList.item() is returning a DOM_Node by value.  The value
that you are trying to cast doesn't have a usable memory
address at that point - it's just a temporary value in the
middle of an expression - so casting it to a reference type
doesn't work.  This is what the "from rvalue of ..." in the
error message means.

Adding an intermediate variable should fix the problem

DOM_Node t = nodList.item(i);
DOM_Element elem = static_cast<DOM_Element &>t;

Andy Heninger
IBM, Cupertino, CA
heninger@us.ibm.com



----- Original Message -----
From: "Ryan Koss" <rk...@acmepacket.com>
To: <xe...@xml.apache.org>
Sent: Monday, April 02, 2001 12:35 PM
Subject: Re: Casting DOM_Node to DOM_Element... still a problem.


> What compiler and OS is this running on because I still get a the
following
> compiler error when I try to do the cast that has been stated in this
> multi-threaded email...  I am running Mandrake linux 7.1 and my compiler
is
> GCC 2.95.3
>
> xmlobject.cpp:446: conversion to non-const reference type `class
DOM_Element
> &'
> xmlobject.cpp:446: from rvalue of type `DOM_Node'
> make: *** [xmlobject.o] Error 1
>
> Here is the code, I am cycling through a list of DOM_Nodes here...
>
>  DOM_Element elem = static_cast<DOM_Element&>(nodeList.item(i));
>
> Any help, ideas, notion, guesses would be greatly appreciated.  Thanks
in
> advance
> -ryan
>



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Casting DOM_Node to DOM_Element... still a problem.

Posted by Ryan Koss <rk...@acmepacket.com>.
What compiler and OS is this running on because I still get a the following
compiler error when I try to do the cast that has been stated in this
multi-threaded email...  I am running Mandrake linux 7.1 and my compiler is
GCC 2.95.3

xmlobject.cpp:446: conversion to non-const reference type `class DOM_Element
&'
xmlobject.cpp:446: from rvalue of type `DOM_Node'
make: *** [xmlobject.o] Error 1

Here is the code, I am cycling through a list of DOM_Nodes here...

 DOM_Element elem = static_cast<DOM_Element&>(nodeList.item(i));

Any help, ideas, notion, guesses would be greatly appreciated.  Thanks in
advance
-ryan

----- Original Message -----
From: "Miroslaw Dobrzanski-Neumann" <mn...@mosaic-ag.com>
To: <xe...@xml.apache.org>
Sent: Friday, March 23, 2001 11:45 AM
Subject: Re: Casting DOM_Node to DOM_Element


> On Fri, Mar 23, 2001 at 09:20:22AM -0500, David E. Cleary wrote:
> >
> >
> > > -----Original Message-----
> > > From: jpcs@progress.com [mailto:jpcs@progress.com]On Behalf Of John
> > > Snelson
> > >
> > > The C++ way of doing this is the following code fragment:
> > >
> > > if(node.getNodeType() == DOM_Node::ELEMENT_NODE) {
> > >   DOM_Element element = static_cast<DOM_Element&>(node);
> > > }
> >
> > Wouldn't dynanic_cast be more what Curt is looking to do, as that will
> > actually perform a runtime check to see in node is indeed a DOM_Element?
> > I've never used these casting constructs myself, so I am interested in
> > hearing other's thoughts on them.
>
> The cleanest way to do casting is using class specific cast operator or
cast
> constructor.
> element = (DOM_Element) node
> is clean (???) cast in Java but it does not work in C++.
> element = static_cast<DOM_Element&>(node);
> says "I do know what I do" (what about the future?)
>
>
> The solution 1 (cast operator):
> class DOM_Node
> {
> ...
> operator DOM_Element(void);
> };
>
> The solution 2 (cast constructor):
> class DOM_Element
> {
> ...
> DOM_Element (const DOM_Node&);
> };
>
> Both solution are better then direct cast because the casts are method
calls
> which can do what they want to guarantee the correct casting. For invalid
> conversion one could throw a appropriate exception. I (and many others as
I
> guess) would wellcome one of them in Xerces DOM.
>
> regards.
> --
> Miroslaw Dobrzanski-Neumann
>
> MOSAIC SOFTWARE AG
> Base Development and Research
> Tel +49-2225-882-291
> Fax +49-2225-882-201
> E-mail: mne@mosaic-ag.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Casting DOM_Node to DOM_Element

Posted by Miroslaw Dobrzanski-Neumann <mn...@mosaic-ag.com>.
On Fri, Mar 23, 2001 at 09:20:22AM -0500, David E. Cleary wrote:
> 
> 
> > -----Original Message-----
> > From: jpcs@progress.com [mailto:jpcs@progress.com]On Behalf Of John
> > Snelson
> >
> > The C++ way of doing this is the following code fragment:
> >
> > if(node.getNodeType() == DOM_Node::ELEMENT_NODE) {
> >   DOM_Element element = static_cast<DOM_Element&>(node);
> > }
> 
> Wouldn't dynanic_cast be more what Curt is looking to do, as that will
> actually perform a runtime check to see in node is indeed a DOM_Element?
> I've never used these casting constructs myself, so I am interested in
> hearing other's thoughts on them.

The cleanest way to do casting is using class specific cast operator or cast
constructor.
	element = (DOM_Element) node
is clean (???) cast in Java but it does not work in C++.
	element = static_cast<DOM_Element&>(node);
says "I do know what I do" (what about the future?)


The solution 1 (cast operator):
class DOM_Node
{
	...
	operator DOM_Element(void);
};

The solution 2 (cast constructor):
class DOM_Element
{
	...
	DOM_Element (const DOM_Node&);
};

Both solution are better then direct cast because the casts are method calls
which can do what they want to guarantee the correct casting. For invalid
conversion one could throw a appropriate exception. I (and many others as I
guess) would wellcome one of them in Xerces DOM.

regards.
-- 
Miroslaw Dobrzanski-Neumann

MOSAIC SOFTWARE AG
Base Development and Research
Tel +49-2225-882-291
Fax +49-2225-882-201
E-mail: mne@mosaic-ag.com


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Casting DOM_Node to DOM_Element

Posted by "David E. Cleary" <da...@progress.com>.

> -----Original Message-----
> From: jpcs@progress.com [mailto:jpcs@progress.com]On Behalf Of John
> Snelson
>
> The C++ way of doing this is the following code fragment:
>
> if(node.getNodeType() == DOM_Node::ELEMENT_NODE) {
>   DOM_Element element = static_cast<DOM_Element&>(node);
> }

Wouldn't dynanic_cast be more what Curt is looking to do, as that will
actually perform a runtime check to see in node is indeed a DOM_Element?
I've never used these casting constructs myself, so I am interested in
hearing other's thoughts on them.

David Cleary


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Casting DOM_Node to DOM_Element

Posted by John Snelson <jp...@decisionsoft.com>.
The C++ way of doing this is the following code fragment:

if(node.getNodeType() == DOM_Node::ELEMENT_NODE) {
  DOM_Element element = static_cast<DOM_Element&>(node);
}


John Snelson

"Arnold, Curt" wrote:
> 
> A very common pattern (at least in my code) when using Xerces-J's DOM is to do something like:
> 
> for(DOM_Node node = element.getFirstChild();
>     node != null;
>     node = node.getNextSibling()) {
>     if(node.getNodeType() == DOM_Node.ELEMENT_NODE) {
>         DOM_Element childElem = (DOM_Element) node;
>         //
>         //  do some element specific behavior
> 
> I haven't found a good way to do the equivalent of:
> 
>         DOM_Element childElem = (DOM_Element) node;
> 
> with Xerces-C.  I think you could do somehting like:
> 
>         DOM_Element childElem = *((DOM_Element*) &node);
> 
> but that seems pretty ugly and may be fatal if you didn't do the preliminary check for getNodeType() == ELEMENT_NODE.
> 
> What I was thinking was that adding a constructor for DOM_Element that took a DOM_Node& and would throw an exception if the node wasn't appropriate would allow you to keep the Java pattern.  (Of
> course, you would want to do this for all the other DOM_Comment, DOM_ProcessingInstruction, et al).
> 
> Maybe this could go into the new DOM implementation?

-- 
John Snelson, Software Engineer       DecisionSoft Ltd.
Telephone: +44-1865-203192            http://www.decisionsoft.com

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org