Looping through attributes

View: New views
10 Messages — Rating Filter:   Alert me  

Looping through attributes

by bashiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Hello folks,
There is one thing bodering me and I hope someone could help.
When I loop through attributes to search for values. How do I stop
jdom from not printing on any attribute that it loops through.

Example:
I search for an attribute "domain" when the corresponding value matches
jdom returns found. But when not found; it prints on all attributes
and I get a list of not found, not found, corresponding to the number of attributes in the documents.

What code or how do i tell jdom to print "not found" only one time?

Bashiro
Drammen-Norway


_______________________________________________
No banners. No pop-ups. No kidding.
Make My Way  your home on the Web - http://www.myway.com


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

RE: Looping through attributes

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Show us your input, show us your code, and we can help you understand where
you have gone wrong. JDOM doesn't do anything unless you ask it to.

Michael Kay
http://www.saxonica.com/

>
> Hello folks,
> There is one thing bodering me and I hope someone could help.
> When I loop through attributes to search for values. How do I
> stop jdom from not printing on any attribute that it loops through.
>
> Example:
> I search for an attribute "domain" when the corresponding
> value matches jdom returns found. But when not found; it
> prints on all attributes and I get a list of not found, not
> found, corresponding to the number of attributes in the documents.
>
> What code or how do i tell jdom to print "not found" only one time?
>
> Bashiro
> Drammen-Norway
>
>
> _______________________________________________
> No banners. No pop-ups. No kidding.
> Make My Way  your home on the Web - http://www.myway.com
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you
> rhost.com

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Parent Message unknown RE: Looping through attributes

by bashiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Hello,
Thanks for the reply. I appreciate this;

here is the snip code;
 

public  void searchDomain(Element element, String dom) {
   
    List attributes = element.getAttributes();
   
    if (!attributes.isEmpty()) {
      Iterator iterator = attributes.iterator();
      while (iterator.hasNext()) {
        Attribute attribute = (Attribute) iterator.next();
        String name = attribute.getName();
        String value = attribute.getValue();
 
            if(name.equals("domain")&&value.equals(dom.trim())){
             jTextArea1.append(" Domain Exists: " + value + "\n");
             return;
            }
   
        else {
           
          System.out.println("Domain does not exist  " +(dom));      
           }
        }
       
    }

  }

Thanks
Bashiro
Drammen-Norway

 --- On Mon 01/21, Michael Kay < mike@... > wrote:
From: Michael Kay [mailto: mike@...]
To: bashiro@..., jdom-interest@...
Date: Mon, 21 Jan 2008 21:09:29 -0000
Subject: RE: [jdom-interest] Looping through attributes

Show us your input, show us your code, and we can help you understand whereyou have gone wrong. JDOM doesn't do anything unless you ask it to.Michael Kayhttp://www.saxonica.com/> > Hello folks,> There is one thing bodering me and I hope someone could help.> When I loop through attributes to search for values. How do I > stop jdom from not printing on any attribute that it loops through.> > Example:> I search for an attribute "domain" when the corresponding > value matches jdom returns found. But when not found; it > prints on all attributes and I get a list of not found, not > found, corresponding to the number of attributes in the documents.> > What code or how do i tell jdom to print "not found" only one time?> > Bashiro> Drammen-Norway> > > _______________________________________________> No banners. No pop-ups. No kidding.> Make My Way  your home on the Web - http://www.myway.com> > > _______________________________________________> To control your jdom-interest
membership:> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you> rhost.com

_______________________________________________
No banners. No pop-ups. No kidding.
Make My Way  your home on the Web - http://www.myway.com


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

RE: Looping through attributes

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I can't see why you aren't using getAttribute() to get the attribute by
name, which would surely be much simpler. But your actual bug is that you
are executing the "else" code ("Domain does not exist") once for every
non-matching attribute (it's inside the while loop). A dangling else bug.
Take more care over indentation in future.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: jdom-interest-bounces@...
> [mailto:jdom-interest-bounces@...] On Behalf Of Bashiro
> Sent: 22 January 2008 02:01
> To: jdom-interest@...
> Subject: RE: [jdom-interest] Looping through attributes
>
>
>
> Hello,
> Thanks for the reply. I appreciate this;
>
> here is the snip code;
>  
>
> public  void searchDomain(Element element, String dom) {
>    
>     List attributes = element.getAttributes();
>    
>     if (!attributes.isEmpty()) {
>       Iterator iterator = attributes.iterator();
>       while (iterator.hasNext()) {
>         Attribute attribute = (Attribute) iterator.next();
>         String name = attribute.getName();
>         String value = attribute.getValue();
>  
>             if(name.equals("domain")&&value.equals(dom.trim())){
>              jTextArea1.append(" Domain Exists: " + value + "\n");
>              return;
>             }
>    
>         else {
>            
>           System.out.println("Domain does not exist  " +(dom));      
>            }
>         }
>        
>     }
>
>   }
>
> Thanks
> Bashiro
> Drammen-Norway
>
>  --- On Mon 01/21, Michael Kay < mike@... > wrote:
> From: Michael Kay [mailto: mike@...]
> To: bashiro@..., jdom-interest@...
> Date: Mon, 21 Jan 2008 21:09:29 -0000
> Subject: RE: [jdom-interest] Looping through attributes
>
> Show us your input, show us your code, and we can help you
> understand whereyou have gone wrong. JDOM doesn't do anything
> unless you ask it to.Michael Kayhttp://www.saxonica.com/> >
> Hello folks,> There is one thing bodering me and I hope
> someone could help.> When I loop through attributes to search
> for values. How do I > stop jdom from not printing on any
> attribute that it loops through.> > Example:> I search for an
> attribute "domain" when the corresponding > value matches
> jdom returns found. But when not found; it > prints on all
> attributes and I get a list of not found, not > found,
> corresponding to the number of attributes in the documents.>
> > What code or how do i tell jdom to print "not found" only
> one time?> > Bashiro> Drammen-Norway> > >
> _______________________________________________> No banners.
> No pop-ups. No kidding.> Make My Way  your home on the Web -
> http://www.myway.com> > >
> _______________________________________________> To control
> your jdom-interest membership:>
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you
> > rhost.com
>
> _______________________________________________
> No banners. No pop-ups. No kidding.
> Make My Way  your home on the Web - http://www.myway.com
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you
> rhost.com

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Parent Message unknown RE: Looping through attributes

by bashiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks for the mail.
And thanks for warning me about the else clause.
Do you have any suggestions on how to let jdom
display a message when the attribute is not dound ?

Thanks
Bashiro
Drammen-Norway

 --- On Tue 01/22, Michael Kay < mike@... > wrote:
From: Michael Kay [mailto: mike@...]
To: bashiro@..., jdom-interest@...
Date: Tue, 22 Jan 2008 10:13:31 -0000
Subject: RE: [jdom-interest] Looping through attributes

I can't see why you aren't using getAttribute() to get the attribute byname, which would surely be much simpler. But your actual bug is that youare executing the "else" code ("Domain does not exist") once for everynon-matching attribute (it's inside the while loop). A dangling else bug.Take more care over indentation in future.Michael Kayhttp://www.saxonica.com/> -----Original Message-----> From: jdom-interest-bounces@... > [mailto:jdom-interest-bounces@...] On Behalf Of Bashiro> Sent: 22 January 2008 02:01> To: jdom-interest@...> Subject: RE: [jdom-interest] Looping through attributes> > > > Hello,> Thanks for the reply. I appreciate this;> > here is the snip code;>   > > public  void searchDomain(Element element, String dom) {>     >     List attributes = element.getAttributes();>     >     if (!attributes.isEmpty()) {>       Iterator iterator = attributes.iterator();>       while (iterator.hasNext()) {>         Attribute attribute = (Attribute)
iterator.next();>         String name = attribute.getName();>         String value = attribute.getValue();>   >             if(name.equals("domain")&&value.equals(dom.trim())){>              jTextArea1.append(" Domain Exists: " + value + "\n"); >              return;>             }>     >         else {>            >           System.out.println("Domain does not exist  " +(dom));      >            } >         }>        >     }> >   }> > Thanks> Bashiro> Drammen-Norway> >  --- On Mon 01/21, Michael Kay < mike@... > wrote:> From: Michael Kay [mailto: mike@...]> To: bashiro@..., jdom-interest@...> Date: Mon, 21 Jan 2008 21:09:29 -0000> Subject: RE: [jdom-interest] Looping through attributes> > Show us your input, show us your code, and we can help you > understand whereyou have gone wrong. JDOM doesn't do anything > unless you ask it to.Michael Kayhttp://www.saxonica.com/> > > Hello folks,> There is one thing bodering me and I hope > someone could
help.> When I loop through attributes to search > for values. How do I > stop jdom from not printing on any > attribute that it loops through.> > Example:> I search for an > attribute "domain" when the corresponding > value matches > jdom returns found. But when not found; it > prints on all > attributes and I get a list of not found, not > found, > corresponding to the number of attributes in the documents.> > > What code or how do i tell jdom to print "not found" only > one time?> > Bashiro> Drammen-Norway> > > > _______________________________________________> No banners. > No pop-ups. No kidding.> Make My Way  your home on the Web - > http://www.myway.com> > > > _______________________________________________> To control > your jdom-interest membership:> > http://www.jdom.org/mailman/options/jdom-interest/youraddr@you> > rhost.com> > _______________________________________________> No banners. No pop-ups. No kidding.> Make My Way  your home on the Web -
http://www.myway.com> > > _______________________________________________> To control your jdom-interest membership:> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you> rhost.com

_______________________________________________
No banners. No pop-ups. No kidding.
Make My Way  your home on the Web - http://www.myway.com


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

RE: Looping through attributes

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You surely don't want JDOM to display the message, you want to do it
yourself.

Isn't it just

  Attribute att = element.getAttribute(name);
  if (att == null) {
    display message;
  }

or have I missed the point?

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: jdom-interest-bounces@...
> [mailto:jdom-interest-bounces@...] On Behalf Of Bashiro
> Sent: 22 January 2008 17:53
> To: jdom-interest@...
> Subject: RE: [jdom-interest] Looping through attributes
>
>
> Thanks for the mail.
> And thanks for warning me about the else clause.
> Do you have any suggestions on how to let jdom display a
> message when the attribute is not dound ?
>
> Thanks
> Bashiro
> Drammen-Norway
>
>  --- On Tue 01/22, Michael Kay < mike@... > wrote:
> From: Michael Kay [mailto: mike@...]
> To: bashiro@..., jdom-interest@...
> Date: Tue, 22 Jan 2008 10:13:31 -0000
> Subject: RE: [jdom-interest] Looping through attributes
>
> I can't see why you aren't using getAttribute() to get the
> attribute byname, which would surely be much simpler. But
> your actual bug is that youare executing the "else" code
> ("Domain does not exist") once for everynon-matching
> attribute (it's inside the while loop). A dangling else
> bug.Take more care over indentation in future.Michael
> Kayhttp://www.saxonica.com/> -----Original Message----->
> From: jdom-interest-bounces@... >
> [mailto:jdom-interest-bounces@...] On Behalf Of Bashiro>
> Sent: 22 January 2008 02:01> To: jdom-interest@...>
> Subject: RE: [jdom-interest] Looping through attributes> > >
> > Hello,> Thanks for the reply. I appreciate this;> > here is
> the snip code;>   > > public  void searchDomain(Element
> element, String dom) {>     >     List attributes =
> element.getAttributes();>     >     if
> (!attributes.isEmpty()) {>       Iterator iterator =
> attributes.iterator();>       while (iterator.hasNext()) {>  
>       Attribute attribute = (Attribute)
> iterator.next();>         String name = attribute.getName();>
>         String value = attribute.getValue();>   >            
> if(name.equals("domain")&&value.equals(dom.trim())){>        
>      jTextArea1.append(" Domain Exists: " + value + "\n"); >  
>             return;>             }>     >         else {>    
>        >           System.out.println("Domain does not exist  
> " +(dom));      >            } >         }>        >     }> >
>   }> > Thanks> Bashiro> Drammen-Norway> >  --- On Mon 01/21,
> Michael Kay < mike@... > wrote:> From: Michael Kay
> [mailto: mike@...]> To: bashiro@...,
> jdom-interest@...> Date: Mon, 21 Jan 2008 21:09:29
> -0000> Subject: RE: [jdom-interest] Looping through
> attributes> > Show us your input, show us your code, and we
> can help you > understand whereyou have gone wrong. JDOM
> doesn't do anything > unless you ask it to.Michael
> Kayhttp://www.saxonica.com/> > > Hello folks,> There is one
> thing bodering me and I hope > someone could
> help.> When I loop through attributes to search > for values.
> How do I > stop jdom from not printing on any > attribute
> that it loops through.> > Example:> I search for an >
> attribute "domain" when the corresponding > value matches >
> jdom returns found. But when not found; it > prints on all >
> attributes and I get a list of not found, not > found, >
> corresponding to the number of attributes in the documents.>
> > > What code or how do i tell jdom to print "not found" only
> > one time?> > Bashiro> Drammen-Norway> > > >
> _______________________________________________> No banners.
> > No pop-ups. No kidding.> Make My Way  your home on the Web
> - > http://www.myway.com> > > >
> _______________________________________________> To control >
> your jdom-interest membership:> >
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you
> > > rhost.com> >
> _______________________________________________> No banners.
> No pop-ups. No kidding.> Make My Way  your home on the Web -
> http://www.myway.com> > >
> _______________________________________________> To control
> your jdom-interest membership:>
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you
> > rhost.com
>
> _______________________________________________
> No banners. No pop-ups. No kidding.
> Make My Way  your home on the Web - http://www.myway.com
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you
> rhost.com

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Re: Looping through attributes

by Grzegorz Kaczor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

>
> Thanks for the mail.
> And thanks for warning me about the else clause.
> Do you have any suggestions on how to let jdom
> display a message when the attribute is not dound ?

To have a boolean variable 'found' initialized to false before the
loop, set it to true on find and use 'break' instead of 'return'? Just
a suggestion.

Grzegorz
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Parent Message unknown RE: Looping through attributes

by bashiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Thanks again Mike!
I am trying to get the value of the attr and not the attr itself.
I have a list of domains and the corresponding email adresses.
So when I get the "domain" as "attr" then get the corresponding value (that is:the domain name itself entered by the user).
and then display the domain and email address.

So if I use;
 if (att == null) {
display message;
}
it displays nothing



Bashiro
Drammen-Norway


 --- On Tue 01/22, Michael Kay < mike@... > wrote:
From: Michael Kay [mailto: mike@...]
To: bashiro@..., jdom-interest@...
Date: Tue, 22 Jan 2008 19:08:19 -0000
Subject: RE: [jdom-interest] Looping through attributes

You surely don't want JDOM to display the message, you want to do ityourself.Isn't it just   Attribute att = element.getAttribute(name);  if (att == null) {    display message;  }or have I missed the point?Michael Kayhttp://www.saxonica.com/ > -----Original Message-----> From: jdom-interest-bounces@... > [mailto:jdom-interest-bounces@...] On Behalf Of Bashiro> Sent: 22 January 2008 17:53> To: jdom-interest@...> Subject: RE: [jdom-interest] Looping through attributes> > > Thanks for the mail.> And thanks for warning me about the else clause.> Do you have any suggestions on how to let jdom display a > message when the attribute is not dound ?> > Thanks> Bashiro> Drammen-Norway> >  --- On Tue 01/22, Michael Kay < mike@... > wrote:> From: Michael Kay [mailto: mike@...]> To: bashiro@..., jdom-interest@...> Date: Tue, 22 Jan 2008 10:13:31 -0000> Subject: RE: [jdom-interest] Looping through attributes> > I can't see why you aren't using
getAttribute() to get the > attribute byname, which would surely be much simpler. But > your actual bug is that youare executing the "else" code > ("Domain does not exist") once for everynon-matching > attribute (it's inside the while loop). A dangling else > bug.Take more care over indentation in future.Michael > Kayhttp://www.saxonica.com/> -----Original Message-----> > From: jdom-interest-bounces@... > > [mailto:jdom-interest-bounces@...] On Behalf Of Bashiro> > Sent: 22 January 2008 02:01> To: jdom-interest@...> > Subject: RE: [jdom-interest] Looping through attributes> > > > > Hello,> Thanks for the reply. I appreciate this;> > here is > the snip code;>   > > public  void searchDomain(Element > element, String dom) {>     >     List attributes = > element.getAttributes();>     >     if > (!attributes.isEmpty()) {>       Iterator iterator = > attributes.iterator();>       while (iterator.hasNext()) {>   >       Attribute attribute = (Attribute) >
iterator.next();>         String name = attribute.getName();> >         String value = attribute.getValue();>   >             > if(name.equals("domain")&&value.equals(dom.trim())){>         >      jTextArea1.append(" Domain Exists: " + value + "\n"); >  >             return;>             }>     >         else {>     >        >           System.out.println("Domain does not exist  > " +(dom));      >            } >         }>        >     }> > >   }> > Thanks> Bashiro> Drammen-Norway> >  --- On Mon 01/21, > Michael Kay < mike@... > wrote:> From: Michael Kay > [mailto: mike@...]> To: bashiro@..., > jdom-interest@...> Date: Mon, 21 Jan 2008 21:09:29 > -0000> Subject: RE: [jdom-interest] Looping through > attributes> > Show us your input, show us your code, and we > can help you > understand whereyou have gone wrong. JDOM > doesn't do anything > unless you ask it to.Michael > Kayhttp://www.saxonica.com/> > > Hello folks,> There is one > thing bodering
me and I hope > someone could > help.> When I loop through attributes to search > for values. > How do I > stop jdom from not printing on any > attribute > that it loops through.> > Example:> I search for an > > attribute "domain" when the corresponding > value matches > > jdom returns found. But when not found; it > prints on all > > attributes and I get a list of not found, not > found, > > corresponding to the number of attributes in the documents.> > > > What code or how do i tell jdom to print "not found" only > > one time?> > Bashiro> Drammen-Norway> > > > > _______________________________________________> No banners. > > No pop-ups. No kidding.> Make My Way  your home on the Web > - > http://www.myway.com> > > > > _______________________________________________> To control > > your jdom-interest membership:> > > http://www.jdom.org/mailman/options/jdom-interest/youraddr@you> > > rhost.com> > > _______________________________________________> No banners. > No pop-ups.
No kidding.> Make My Way  your home on the Web - > http://www.myway.com> > > > _______________________________________________> To control > your jdom-interest membership:> > http://www.jdom.org/mailman/options/jdom-interest/youraddr@you> > rhost.com> > _______________________________________________> No banners. No pop-ups. No kidding.> Make My Way  your home on the Web - http://www.myway.com> > > _______________________________________________> To control your jdom-interest membership:> http://www.jdom.org/mailman/options/jdom-interest/youraddr@you> rhost.com

_______________________________________________
No banners. No pop-ups. No kidding.
Make My Way  your home on the Web - http://www.myway.com


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Parent Message unknown Re: Looping through attributes

by bashiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Thanks,

Theoriticaly yes, but practically no!
When boolean is false, that is when the value of an attribute is not found,
jdom displays as many "not founds" as the number of contens in the xml.

I am beguining to think if I should combine HashMap so that Hashmap will check before passing it on to the jdom code.
I thought jdom could do this easier....

Bashiro
Drammen-Norway




 --- On Tue 01/22, Grzegorz Kaczor < grzegorz.kaczor@... > wrote:
From: Grzegorz Kaczor [mailto: grzegorz.kaczor@...]
To: bashiro@...
     Cc: jdom-interest@...
Date: Tue, 22 Jan 2008 20:33:37 +0100
Subject: Re: [jdom-interest] Looping through attributes

Hi>> Thanks for the mail.> And thanks for warning me about the else clause.> Do you have any suggestions on how to let jdom> display a message when the attribute is not dound ?To have a boolean variable 'found' initialized to false before theloop, set it to true on find and use 'break' instead of 'return'? Justa suggestion.Grzegorz

_______________________________________________
No banners. No pop-ups. No kidding.
Make My Way  your home on the Web - http://www.myway.com


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Re: Looping through attributes

by Grzegorz Kaczor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

No :).
boolean found = falsefor (...)  {  if ("domain".equals(...)) {     ...     found = true;     break;  }}
So not found will be displayed once.
But better use Michael's way, it is ok.
String value = element.getAtttibute("domain");if (value == null || (!value.equals(dom.trim()))) display "not found"
Regards,Grzegorz
2008/1/22, Bashiro <bashiro@...>:>>> Thanks,>> Theoriticaly yes, but practically no!> When boolean is false, that is when the value of an attribute is not found,> jdom displays as many "not founds" as the number of contens in the xml.>> I am beguining to think if I should combine HashMap so that Hashmap will check before passing it on to the jdom code.> I thought jdom could do this easier....>> Bashiro> Drammen-Norway>>>>>  --- On Tue 01/22, Grzegorz Kaczor < grzegorz.kaczor@... > wrote:> From: Grzegorz Kaczor [mailto: grzegorz.kaczor@...]> To: bashiro@...>      Cc: jdom-interest@...> Date: Tue, 22 Jan 2008 20:33:37 +0100> Subject: Re: [jdom-interest] Looping through attributes>> Hi>> Thanks for the mail.> And thanks for warning me about the else clause.> Do you have any suggestions on how to let jdom> display a message when the attribute is not dound ?To have a boolean variable 'found' initialized to false before theloop, set it to true on find and use 'break' instead of 'return'? Justa suggestion.Grzegorz>> _______________________________________________> No banners. No pop-ups. No kidding.> Make My Way  your home on the Web - http://www.myway.com>>> _______________________________________________> To control your jdom-interest membership:> http://www.jdom.org/mailman/options/jdom-interest/youraddr@...>

-- "Choć tyle wiemy własnym doświadczeniem:W nas jest Raj, Piekło - i do obu - szlaki."J.K.
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...