This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 22572
122
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
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic