Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing with DOM

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
EU
hi,
im trying to get information out of an xml file and i have to use DOM. i havnt found any tutorials on the net that are any good.

if any of ye know of any good ones that would be appreciated.

this is the structure of the file.
Code:
<?xml version='1.0' encoding='UTF-8'?>
<ROLEDEFINITIONS>

	<ROLE>
	
		<ROLENAME>SuperAdministrator</ROLENAME>
		
		<CONFIGDETAILS>
	
			<CONFIGNAME>Administrator</CONFIGNAME>
			
			<PERMISSIONS>
	
				<PERMISSIONNAME>Create</PERMISSIONNAME>
				<PERMISSIONNAME>Modify</PERMISSIONNAME>
				<PERMISSIONNAME>Delete</PERMISSIONNAME>
				<PERMISSIONNAME>Details</PERMISSIONNAME>
				<PERMISSIONNAME>List</PERMISSIONNAME>
	
			</PERMISSIONS>
	
		</CONFIGDETAILS>
		
		<CONFIGDETAILS>
	
			<CONFIGNAME>Designer</CONFIGNAME>
		
			<PERMISSIONS>
	
				<PERMISSIONNAME>Create</PERMISSIONNAME>
				<PERMISSIONNAME>Modify</PERMISSIONNAME>
				<PERMISSIONNAME>Delete</PERMISSIONNAME>
				<PERMISSIONNAME>Details</PERMISSIONNAME>
				<PERMISSIONNAME>List</PERMISSIONNAME>
				
			</PERMISSIONS>
	
		</CONFIGDETAILS>
	
	</ROLE>
	
	<ROLE>
	
		<ROLENAME>SuperDesigner</ROLENAME>
		
		<CONFIGDETAILS>
	
			<CONFIGNAME>Designer</CONFIGNAME>
		
			<PERMISSIONS>
	
				<PERMISSIONNAME>Create</PERMISSIONNAME>
				<PERMISSIONNAME>Modify</PERMISSIONNAME>
				<PERMISSIONNAME>Delete</PERMISSIONNAME>
				<PERMISSIONNAME>Details</PERMISSIONNAME>
				<PERMISSIONNAME>List</PERMISSIONNAME>
	
			</PERMISSIONS>
	
		</CONFIGDETAILS>
	
	</ROLE>
	
	<ROLE>
	
		<ROLENAME>SuperUser</ROLENAME>
		
		<CONFIGDETAILS>
	
			<CONFIGNAME>Administrator</CONFIGNAME>
		
			<PERMISSIONS>
	
				<PERMISSIONNAME>Modify</PERMISSIONNAME>
				<PERMISSIONNAME>Details</PERMISSIONNAME>
				<PERMISSIONNAME>List</PERMISSIONNAME>
	
			</PERMISSIONS>
	
		</CONFIGDETAILS>
	
		<CONFIGDETAILS>
	
			<CONFIGNAME>Designer</CONFIGNAME>
		
			<PERMISSIONS>
	
				<PERMISSIONNAME>Modify</PERMISSIONNAME>
				<PERMISSIONNAME>Details</PERMISSIONNAME>
				<PERMISSIONNAME>List</PERMISSIONNAME>
	
			</PERMISSIONS>
	
		</CONFIGDETAILS>
	
	</ROLE>
	
</ROLEDEFINITIONS>

in the above xml file:
many Roles
one role name
many config details
one config name
one permissions
many permission names


ive set up a documnetBuilderFactory.
ive set up a Document object that stores the various elements of the xml file.

im using eclipse to debug it and the the object contains all the information i need.

the problem im having is i cant find any way to gather this information.

i can find the information by using
Code:
String strRoleNameTAG = "ROLENAME";

NodeList ndLstRole = doc.getElementsByTagName(strRoleTAG);

ive set up a few loops that will find the ROLENAME's.

the problem im having using this method is that i cant get the children specifically to the role name e.g. SuperAdministrator and find the config details and the permissions specific to that rola name.

thanks for your help
 
cheers,

what i need to do is initially find the role name. e.g. super administrator.

then find the rest of the information relating to that role and no other.

i then need to find the names of the config details, there can be many of these. then find the permissions of each of the config details seperatly.

all these values will be used both for display and for checking and other such things
 
You need to use a technology called XPath to navigate thru the XML document tree to get to your role name. This will return all role names:

ROLEDEFINITIONS/ROLE/ROLENAME/text()

or more concisely:

//ROLENAME/text()

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
use xpath such as this to select the Role Node...
//ROLE[ROLENAME = 'SuperAdministrator']

With DOM, use:
Nodes = SelectNodes(XPathString)
Or:
Nodes = SelectSingleNode(XPathString)

Such as...
Code:
X = xmlDom.SelectSingleNode("//ROLE[ROLENAME = 'SuperAdministrator']")
Permissions = X.SelectNodes("//PERMISSIONNAME")

Read more about XPath here:

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
cheers for the help lads.
i got it working using a combination of elements, nodes and nodelists. it required a small bit more code but i understand it lot better now.

ive come accross a problem when i try to run the code from jsp.

here is where the problem is.

the Document doc is comming out as null so i cant use any of the rest of my code.

the xml file can be found and the DocumnetBuilderFactory and the DocumentBuilder all have the required information.

is there a problem with the Document object and jsp for reading xml? as the code runs fine from withing a java main method.

thanks for the help

dex
Code:
File fileXMLFile = new File("roles.xml");
            DocumentBuilderFactory dbfDocFactory = DocumentBuilderFactory.newInstance(); // creates the doc builder factory
           DocumentBuilder dbDocBuilder = dbfDocFactory.newDocumentBuilder();

doc = dbDocBuilder.parse(fileXMLFile);				  // this is used read the tags from the xml file
 
could be a security issue. check where you are reading the xml from...

m

 
im using tomcat 5.0.28

the xml file is being called from
WEB-INF/classes
and thats where its located.

would that be an issiue?
 
you might need to load it using resources rather than the normal "file open" method..

the location looks ok. but new File needs replacing with an open resource:

Code:
InputStream is = config.getServletContext().getResourceAsStream("/roles.xml");

doc = dbDocBuilder.parse(is);

or something like that.

matt

 
sound,
what object type is config. ive imported (eclipse suggest them)
com.sun.security.auth.login.ConfigFile
and
sun.security.krb5.Config;

but neither contain the getServletContext() methods.

thanks for your help
 
no tis the ServletConfig class.

Code:
GenericServlet.getServletConfig().getServletContext().getResourceAsStream("/resource.xml");

m

 
Hint: a JSP overrides the GenericServlet object.... just in case you ask how to go about instantiating it.

.

 
i got that working. the problem was with the actual xml file. when i ran it in java it was able to find it in the tomcat/webapp etc/ classes directory, but when i ran the jsp page it was looking for it in

c:\windows\system32

i moved the file there and it worked.
do you know a way for the class to find the file without having to hardcode it?
"../roles.xml" works fine in java but as far as jsp is concerned its not.

thanks for the help

dex
 
you should NOT be accessing files in c:\windows\system32?!

Theres something wrong there.

You've lost me slightly, did you mean "opening with new File" now works or did you get opening a resource working as I suggested?

Really, this should move to a java forum now ;)

M

 
well it confused me. cant see any reason why it should be looking for c:\windows\system32

i copied the original file over to the above location. but this isnt really what i need.

while i was having a look at opening the resource working i put in bits of code to debug the jsp and thats when i found that the xml file, somehow was expected to be found in the system32 folder.

i think ill take your advice n repost to a jsp forum instead. see if anyone has had the same bizzare problem.

thanks for your help matt, much appreciated
:)

dex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top