• 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

Sax Parser

 
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand how Sax Parsers work - however, all examples and such I have seen get all the tag elements. For example the file will look like this:



It will be coded to retrieve all of this -however I want to get just the employee id and nothing else - possible using Sax Parser?
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alice Hampton wrote:however I want to get just the employee id and nothing else - possible using Sax Parser?


Yes. You just ignore everything you don't want.
You could use DOM and then just get the bits you want from the tree, but it's still going to have to read the whole xml file.
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you wouldn't mind could you show me? I think I have it but I could be off a little or I could show you my code and you could tell me if its wrong? (If you dont mind that is)
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Sorry about that - no it doesn't it was just an example I had been looking at. I just wanted to ask a question so I used it as reference Also thank you for changing tags!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It will be coded to retrieve all of this -however I want to get just the employee id and nothing else - possible using Sax Parser?



See the JavaDocs for org.xml.sax.helpers - the DefaultHandler class

The startElement method gets an event with the name of the Element and a reference to an "Attributes" list - the value for "id" will be in Attributes.

Bill
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alice Hampton wrote:I could show you my code and you could tell me if its wrong? (If you dont mind that is)


That would be the best option. It's easier to explain things once we know your current level of understanding and it also means we don't waste time explaining stuff you already know.
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats what I have so far:




I want to just obtain the environment id from the xml so I can display it in a drop down list
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alice Hampton wrote:I want to just obtain the environment id from the xml so I can display it in a drop down list


Are you sure ? In your original post you wanted the employee id.
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes - mine is for environment id, the first code I posted (not sure if you saw me say) was just an example on the internet
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alice Hampton wrote:Yes - mine is for environment id, the first code I posted (not sure if you saw me say) was just an example on the internet


Okay. I missed that.
Your code looks fine as far as it goes. What comes next ? You've created a parser and a default handler. How are you using them ?
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to check is my code as whole okay here? I just gave a snippet before. Thankyou for your help - really appreciate it!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, it's appears okay as far as it goes, but all you are doing is creating some objects (a SAXParser and a Defaulthandler). You're not actually doing anything with those objects
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanted to make sure with it being within my main method that it was okay. Got to work out where to go from here now :P
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alice Hampton wrote:I just wanted to make sure with it being within my main method that it was okay.


In that case, it's definitely not okay, but as you'd already changed it in your other thread I assumed you already knew that. I was just looking at the actual functionality of the code.
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So should I remove it from my main?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did have it like this:



Is that any better?.....
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alice Hampton wrote:Is that any better?.....


A good start.
A few things.
  • Your XmlWriter class doesn't need to extend DefaultHandler.
  • Normal convention is to start method names with lower case characters.
  • And obviously you will need to call the saxParse method from your main method.
  • Personally I only use anonymous classes where the code is fairly simple and I'm only overriding one (maybe two) methods. For something that has the potential to get complicated (such as the DefaultHandler for a SAXParser) I would create a separate class.
  • I know you haven't started using it yet, but I suspect your myIDList variable will need to become an instance variable rather than being local to main.
  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic