• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SAX parser

 
naga eswar
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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.



 
Tim Moores
Bartender
Posts: 2895
46
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 124
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • 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: 124
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 124
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 124
  • Mark post as helpful
  • send pies
  • 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).





 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • 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: 124
  • Mark post as helpful
  • send pies
  • 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.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • 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
  • 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: 124
  • Mark post as helpful
  • send pies
  • 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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 124
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • 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: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow its worked. Thanks for each and every body who helped....

Thanks a lot....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic