• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

regarding xml parsing and retrieve the maximum and minimum values from that parse

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi frends,
actually i had very huge amount of xml content that has to be parsed and inserted into the database table through jdbc programming.
i am getting the problem in simple java coding that in a xml tag
there are so many fields
ex:
<employees>
<salary>
<price>24000</price>
</salary>
<salary>
<price>2000</price>
</salary>
<salary>
<price>3000</price>
</salary>
<salary>
<price>6000</price>
</salary>

like this the data is there in xml content
and here after parsing is properly completed,but the problem is that i have to get the highest and lowest salaries to insert into the database table.
can u give me any suggestion.....

i think it will done through by using Comparator,but i am not getting
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you do not give enough information... how do you parse ?/ how do you try to find which one is biggest and which one is smallest ? can you show some code ?

do you know that there are two ways to work with XML (SAX/DOM) ?
i would suggest you use SAX which is event driven and more efficient when your documents are huge. write a handler that is triggered when a "price" element occurs.
then you can do any calculation you want.

since XML is "String" based you need to convert the string to a numeric type first (long/double/int...): e.g. Integer.parseInt("12345");

then you can compare if its bigger or smaller than your current "biggest"/"smallest" number:

something like



pascal
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there maybe a thousand ways to to that, depending on the context of what you really want to do.

At first sight, I thought you could use XPath to get just the salay nodes.

You coud use //employees/salary/price to get all the salay nodes, or //*[name()='price'] if actually your XML file is more complicated than what you wrote up there.

Once you have all the salary nodes, visit every node to determine which one is the biggest and which one the smallest, or put them all into a TreeSet and get the head and the tail, or put them into a List and use Collections.max() and Collections.min() method, or put them into an array and use Arrays.sort() and get the first and last elements of the array...
 
reply
    Bookmark Topic Watch Topic
  • New Topic