i am new to XML, i want to know how to read XML data in the java class? without hard coding any things in java class. please help me out with some examples.
now i will tell my probelm. 1) I want read the tag names(i.e name,namelength,Beginlength,Contains) dynamically in struts action class, like if one array contains 10 values means we use for loop and we will print all the 10 values,like this i want to read only all the tag names which are in XML file.
2) I want read the tags like name,namelength,Beginlength,Contains etc..... along with the values of the tags in the action class(struts action class),without hardcoding the tag names.
please provide the solution for this problem with some examples.
Thanks Kademane guru
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35242
7
posted
0
You can use either DOM or SAX to do this. But if you want to avoid referring to element names, you will need to go by relative positions, which is an unstable thing to do. Why do you want to avoid using element names? If you don't want to hardcode them, read them from a property file instead.
kademane guru
Greenhorn
Joined: Feb 20, 2007
Posts: 21
posted
0
Hi Ulf Dittmer sir, thanks for reply. but i dont know, how to read the XML Tags from the proeprty file. please guide me how to do this ?
Thanks Kademane guru
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35242
7
posted
0
You wouldn't read XML from a property file. What I suggested was a way to avoid hardcoding the names of the XML element in your XML parsing code. You would do that by reading their names from some property file. That file might look like this:
Then you don't have to refer to element "Name" in your code, but instead to the property "element.name". That way you can change the name of the element to (e.g.) "FirstName" later, and don't have to change the code; just the property file. [ April 09, 2007: Message edited by: Ulf Dittmer ]