• 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: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyday,

I have doubt in SAX parser. Is there any method to retrieve child node attribute names of child node of root node.

for Example below example of XML file




Now my requirement, i want to fetch child nodes attribute names(SERVER1,SERVER2,SERVER3,SERVER4) of development and production nodes.

can anybody help me please.



 
Saloon Keeper
Posts: 7585
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you have so far?

Note that SERVER1, SERVER2 etc. are not attributes, but elements.
 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i am really sorry if there is technical words problem.....

I am first time working with this XML and SAX parser.

My requirement is that I want to fetch all child elements of either production or development elements names into java resource based on condition.
By using that child element name we will fetch corresponding values.

If we want to add new system IPaddress then we have to update only XML(no modification has to be done in java file).


My XML code is beow



help me please.

Thanks in advance.
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:What do you have so far?



If you want to use SAX, your search for a solution must have provided you with a skeleton SAX parser; post it here so we can see what changes you have made to it so that it fits your problem.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harold's free online book will be great help to you as a beginner. It is loaded with examples.

Bill
 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for reply.

Here think is that till now I dont have any SAX parser code. Now I want to introduce (first time) into my application.

I have seen in some example in google search while retrieving the value (say 100.200.23.21), they are using element name (SERVER1) by using the code





But i dont want to hard the element name(server1). It should also retrieve programatically
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naga eswar wrote:But i dont want to hard the element name(server1). It should also retrieve programatically



Okay. So use a variable instead of a constant. That is just basic Java programming, nothing to do with XML at all.

By the way, since we're talking about basic Java programming, never do this:



If an exception is thrown, you will have no idea what it is. Your program will just end and that's all. If nothing else, print out the stack trace:

 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.

The above is an example code. we will take care of catch in the application.

Can you eloborate your suggestion please.

We cant do that with the SAX or DOM parser because I want to maintain the server IP addresses in XML documnet and dont want to touch java program if any IP address need to added or remove. Just we have to update XML file.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naga eswar wrote:Can you eloborate your suggestion please.



It's quite difficult to be any more simple than "If you don't want to hard-code a value then don't". Really. If you don't want to write

because you might want to have some value other than "server1" when you actually run the code, then write

and include some code which assigns a suitable value to the variable named "elementName". You learned how to use variables already, I'm sure.
 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes paul. I got it.


But here problem is that how to fetch that element name form XML using java code and assign to the variable(elementName).





 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naga eswar wrote:But here problem is that how to fetch that element name form XML using java code


If you don't know the element name how will you able to get the required information from the XML. You seems to say the element name changes dynamically?

I am not sure but will it won't fail DTD validations for the element? Can you consider having something like below instead of server1, server2?



If you really want server1, server2 then you can read all the child nodes of <development> and just get its text content (ip addresses). This case you should be pretty sure only server1,2 nodes will be present under development - else you might end up reading unnecessary nodes. At least do a String contains check for the string "SERVER".
 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John . first i have to check my XML code because i am first working on this XML

let me show my XML code. Is this is wrong code.



i have two methods (production and develpment) which we pass ip address(say host address) pass as arguments.
In method production we will connect to above XML (through SAX or DAX) and if host address is equal in the ipaddress of production then return "production" else return "NO".
In method development we will connect to above XML (through SAX or DAX) and if host address is equal in the ipaddress of development then return "developement" else return "NO".

PLease help. Is the XML code is correct or not.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
naga

It is not possible for the java code to know nothing about the XML it has to parse.

Ideally, before you start to extract values from it, you would validate the XML against a DTD or XSD. Assuming validation is passed, the code can then extract elements from it with a known format.

If something like server1 can be different, this would be specified in the DTD/XSD.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are the one creating the XML, then you can also writing a DTD to check well-formedness of the XML. If you aren't then ask the schema of the XML for validation.

I think your problem is while retrieving the IP address without explicitly mentioning the node names. Get the child nodes list under production and development and process them.

These 3 API's should help you further - Document, Node and Element
 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James & John, heartfully thanks.

Myself creating this XML.

I am not getting any method to retrieve child element nodes. I tried to fetch based on elements production and element but without child element names(gursrv0402,gursrv056,etc) unable to retrieve the values.
 
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
You need to redesign your xml - xml tags should not contain data, they are metadata, they tell you what sort of data is contained in the xml. Your xml should be something like this
 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Joanne,

Now can you tell me how to fetch value(Ipaddress) of element address using SAX/DOM.
 
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

naga eswar wrote:Now can you tell me how to fetch value(Ipaddress) of element address using SAX/DOM.


This is a good place to start
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A common mistake in handling character data with SAX is to assume that the characters method will return as much character data as possible; the article Joanne linked to mentions that briefly - you can find more details about that here.
 
naga eswar
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow its worked. Thanks for each and every body who helped....

Thanks a lot....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic