XML Newbie Question

D

Dave Garratt

After a great deal of trawling internet resouces I hope someone can give me
advise and maybe if I'm lucky a small example program. I am writing a small
standalone application (mainly as a learning exercise) and so far I have
created a user logon dialog box that verifies a users details against a
mysql database using jdbc. I want to remove the hard coded references to the
database driver and put these details in a simple XML document that can be
read by the application at startup.

I have created a simple document :-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DatabaseParameters SYSTEM "DatabaseParameters.DTD">
<DatabaseParameters>
<jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
<jdbcUsername>root</jdbcUsername>
<jdbcPassword/>
<jdbcConnectString>jdbc:mysql://localhost/commander</jdbcConnectString>
</DatabaseParameters>

I have looked at the mass of Java XML technologies and achived some success
with DOM albeit very long winded and not re-usable code. I have seen
references to XSLT and XPATH which on the face of it seem to offer a simple
way of retrieving data from a file like this by passing a directory (tree)
like structure to some method which points to the element/attribute you want
to access.

For example to get the jdbc driver details from the file I assume I would
need to specify /DatabaseParameters/jdbcDriver as the path.

However all the references I have seen on the web about this technology seem
to want to show me how to convert data from XML to HTML or some such web
server equivalent.

Am I misinterpreting this API - can someone send me a small example that
allows me to read the contents of an element or attribute into a java String
variable.

Failing an example a url to a relevant example would be appreciated.

Many thanks in advance,


Dave G.
 
Y

Yar Hwee Boon

Dave Garratt said:
After a great deal of trawling internet resouces I hope someone can give me
advise and maybe if I'm lucky a small example program. I am writing a small
standalone application (mainly as a learning exercise) and so far I have
created a user logon dialog box that verifies a users details against a
mysql database using jdbc. I want to remove the hard coded references to the
database driver and put these details in a simple XML document that can be
read by the application at startup.

I have created a simple document :-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DatabaseParameters SYSTEM "DatabaseParameters.DTD">
<DatabaseParameters>
<jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
<jdbcUsername>root</jdbcUsername>
<jdbcPassword/>
<jdbcConnectString>jdbc:mysql://localhost/commander</jdbcConnectString>
</DatabaseParameters>

I have looked at the mass of Java XML technologies and achived some success
with DOM albeit very long winded and not re-usable code. I have seen
references to XSLT and XPATH which on the face of it seem to offer a simple
way of retrieving data from a file like this by passing a directory (tree)
like structure to some method which points to the element/attribute you want
to access.

For example to get the jdbc driver details from the file I assume I would
need to specify /DatabaseParameters/jdbcDriver as the path.

However all the references I have seen on the web about this technology seem
to want to show me how to convert data from XML to HTML or some such web
server equivalent.

Am I misinterpreting this API - can someone send me a small example that
allows me to read the contents of an element or attribute into a java String
variable.

Failing an example a url to a relevant example would be appreciated.

Many thanks in advance,


Dave G.


Aside from looking up how to use DOM and XPath, etc, have you
considered using a java properties file instead? XML looks overkill
here. (unless your learning exercise IS about XML parsing).

Hwee Boon
 
J

J

Personally, I would look at dom4j as the framework to deal with xml. Since
it is much more java like framework, handling xml is much simpler. And with
build in xpath support, dom4j handles xpath seemlessly. I started an XML
processing application using dom, then moved to jdom, then finalized on
dom4j. It is fast enough and gives the simplicty of a document tree. It is
great at learning XML development.

J
 
D

Dave Garratt

I've just downloaded it and it certainly looks promising - thanks for the
advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top