You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Iban Torres <ib...@yahoo.es> on 2001/06/08 16:30:42 UTC

Accessing to a specific child Element

Hi all.

    Single question. I'd like to access to a specific child Element. Knowing
that you have one and only one Element with the desired Tag Name, how can I
get 'that' Element?.
    I know that I can use getElementsByTagName(String), but ,as you know it
returns a NodeList, so I have to declare a NodeList.
Any shorter way to do it?

Thanks in advance.


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


Re: Accessing to a specific child Element

Posted by Iban Torres <ib...@yahoo.es>.
This really helps Ragunath.

 I was thinking in an implemented method not in any piece of
code...probably... I am too lazy.

Thanks.


----- Original Message -----
From: "Ragunath Marudhachalam" <rm...@circuitvision.com>
To: <xe...@xml.apache.org>
Cc: <ib...@yahoo.es>
Sent: Friday, June 08, 2001 3:41 PM
Subject: RE: Accessing to a specific child Element


> Hi i wrote a utility function like this, to get the specified node
value...
>
> public static final String getFirstNodeValue(Node p, String s) {
>         Node e = getFirstNode(p, s);
>         Node c = (e != null) ? e.getFirstChild() : null;
>         return (c != null) ? c.getNodeValue() : "";
>         }
>
>
> public static final Node getFirstNode(Node p, String s) {
>         NodeList list = p.getChildNodes();
>         if (list != null) for (int i=0; i<list.getLength(); i++) {
>                 Node node = list.item(i);
>                 if (node.getNodeName().compareTo(s) == 0)
>                     return node;
>                 }
>         return null;
>         }
>
>
> Hope this helps!!!!!
>
>
>
>
>
> -----Original Message-----
> From: Iban Torres [mailto:ibantr@yahoo.es]
> Sent: Friday, June 08, 2001 10:31 AM
> To: xerces-j-user@xml.apache.org
> Subject: Accessing to a specific child Element
>
>
> Hi all.
>
>     Single question. I'd like to access to a specific child Element.
Knowing
> that you have one and only one Element with the desired Tag Name, how can
I
> get 'that' Element?.
>     I know that I can use getElementsByTagName(String), but ,as you know
it
> returns a NodeList, so I have to declare a NodeList.
> Any shorter way to do it?
>
> Thanks in advance.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org


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


RE: Accessing to a specific child Element

Posted by Ragunath Marudhachalam <rm...@circuitvision.com>.
Hi i wrote a utility function like this, to get the specified node value...

public static final String getFirstNodeValue(Node p, String s) {
        Node e = getFirstNode(p, s);
        Node c = (e != null) ? e.getFirstChild() : null;
        return (c != null) ? c.getNodeValue() : "";
        }


public static final Node getFirstNode(Node p, String s) {
        NodeList list = p.getChildNodes();
        if (list != null) for (int i=0; i<list.getLength(); i++) {
                Node node = list.item(i);
                if (node.getNodeName().compareTo(s) == 0)
                    return node;
                }
        return null;
        }


Hope this helps!!!!!





-----Original Message-----
From: Iban Torres [mailto:ibantr@yahoo.es]
Sent: Friday, June 08, 2001 10:31 AM
To: xerces-j-user@xml.apache.org
Subject: Accessing to a specific child Element


Hi all.

    Single question. I'd like to access to a specific child Element. Knowing
that you have one and only one Element with the desired Tag Name, how can I
get 'that' Element?.
    I know that I can use getElementsByTagName(String), but ,as you know it
returns a NodeList, so I have to declare a NodeList.
Any shorter way to do it?

Thanks in advance.


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


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


RE: Accessing to a specific child Element

Posted by Julian Reschke <ju...@gmx.de>.
> From: Zhaohua Meng [mailto:mengzh@hotlens.com]
> Sent: Friday, June 08, 2001 8:08 PM
> To: xerces-j-user@xml.apache.org
> Subject: Re: Accessing to a specific child Element
> 
> 
> If you can be sure that there is only one such Element,
> why don't use this:
> 
> getElementsByTagName(yourUniqueTagName).item(0);
> 
> It should give you the node without declaring a NodeList.

Bad Idea.

getElementsByTagName does a deep search through the whole DOM.


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


Re: Accessing to a specific child Element

Posted by Zhaohua Meng <me...@hotlens.com>.
If you can be sure that there is only one such Element,
why don't use this:

getElementsByTagName(yourUniqueTagName).item(0);

It should give you the node without declaring a NodeList.

-----Original Message-----
From: "Iban Torres" <ib...@yahoo.es>
To: <xe...@xml.apache.org>
Date: Fri, 8 Jun 2001 15:30:42 +0100
Subject: Accessing to a specific child Element

> Hi all.
> 
>     Single question. I'd like to access to a specific child Element.
> Knowing
> that you have one and only one Element with the desired Tag Name, how
> can I
> get 'that' Element?.
>     I know that I can use getElementsByTagName(String), but ,as you
> know it
> returns a NodeList, so I have to declare a NodeList.
> Any shorter way to do it?
> 
> Thanks in advance.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> 

Zhaohua Meng

Hotlens.com Inc.
http://www.hotlens.com

350 Fifth AVE
Suite 3113
New York, NY 10118
Phone: 212-465-1700
Fax:   212-465-1710
email: mengzh@hotlens.com




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