wood burning stoves 2.0*
The moose likes Java in General and the fly likes SAX parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "SAX parser" Watch "SAX parser" New topic
Author

SAX parser

naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
What do you have so far?

Note that SERVER1, SERVER2 etc. are not attributes, but elements.
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Mar 22, 2000
Posts: 12756
    
    5
Harold's free online book will be great help to you as a beginner. It is loaded with examples.

Bill
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 25, 2012
Posts: 120
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 25, 2012
Posts: 120
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Jan 25, 2012
Posts: 120
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

Joined: Nov 09, 2011
Posts: 1012
    
    5

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Jan 25, 2012
Posts: 120
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

Joined: Aug 05, 2005
Posts: 3415
    
  12
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


Joanne
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
Thank you very much Joanne,

Now can you tell me how to fetch value(Ipaddress) of element address using SAX/DOM.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jan 25, 2012
Posts: 120
wow its worked. Thanks for each and every body who helped....

Thanks a lot....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SAX parser
 
Similar Threads
using theTags from an XML File
Parsing a TreeModel structure
Securing Server to Server communication?
Dynamic Xml Parser
How to install same application in multiple servers in websphere netdeployment App Server Environmen