• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Generalize the code?

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,


I am repeating code every time that looks odd and also if there are 100 elements then a long document. How can I generalize the following code?

Thanks & best regards
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, there seems to be a consistent error in your code. Take the first loop. You are assigning the text you get out of each node to the same variable. In other words, only the final value is kept, and the rest are thrown away. Is that really what you want? I would expect that either you should be adding them to a list, or at the least appending them all.

Once you've fixed that, you're right - you can factor out the bits that are the same into a method, with arguments for those things that are different in each case and those things that need to be passed in. For instance, something like:
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The common parts are these:
So turn that into a method, with doc and the XPath expression as parameters and the node value as the return value:
I've chosen to also include the XPath object as a parameter to avoid having to recreate it each time.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Firstly, there seems to be a consistent error in your code. Take the first loop. You are assigning the text you get out of each node to the same variable. In other words, only the final value is kept, and the rest are thrown away.
Is that really what you want? I would expect that either you should be adding them to a list, or at the least appending them all.



No not at all.

Once you've fixed that, you're right


Actually this is the first time I am working with XPath API. Please correct me where I am wrong that'll be your kind enough


Best regards & thanks for helping
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Thanks for your reply.

Its throwing following errors:


Best regards
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to declare nodes within the method, so that it's in scope when you try to use it.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew. Now its compiled and working fine. How will I deal with this:



Thanks again for helping
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic