![]() |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Java Coding Part Four: XML Preferences Now comes the most mysterious part of all: XML. XML is a term which many have heard, but which no one seems to understand. XML is basically a method for encapsulating data with descriptive tags. So, unlike HTML which mostly describes how to format the data, XMLs tags describe what the data is (a price, a link, etc). Why use XML in a preferences file? So the data does not have to reside in any particular order; its tags make it clear what type of data it is. Well-formed, validated XML files are regulated by a descriptor file (DTD). Heres the DTD file for this project. I wont try to explain all the syntax here. For a tutorial on XML, a place to start is IBMs Alphaworks site (http://www-4.ibm.com/software/developer/education/tutorial-prog/writing.html).
Now that we have the descriptor file, it becomes a matter of entering the data into an XML file. I used IBMs free Xeena to create the XML file, although it requires some work to get installed and running properly. The file is just plain text, so it can also be edited with any text editor, if you elect to go that route.
Note that the last entry is an FTP site and that the filename we want to retrieve has a space in it. While this is considered poor form, you can get around it by using the %20 symbol instead of the space. If you wanted to include a username and password the entry would look like:
Likewise with the proxy info, should you require a username and password there:
To parse the XML file from within our Java code, we open the document and parse it using both W3- and IBM-provided classes:
Then we take the Element root (which is actually a node) and traverse the branch, one node at a time and take the data as it comes. In this sample application, I have chosen to allocate an array of SiteObjects (part three); not the most elegant approach, but hey, this is just a sample app, right? We only need a single ProxyObject since that will not change for a given machine. Note that when you declare (instantiate) an array of a custom object type, you have to then allocate memory (instantiate). I chose to do this in the init() function in the final code.
Although this might seem like a bit of work, the one real advantage is that we can take an XML file generated by anyone, using any XML software, as long as they used our DTD file, and the information can be entered in any order. |
||||||||||||||||||||||
![]() |
||||||||||||||||||||||
| Site by Warner Technology Consulting. All material is copyright 1999. If you reference material on this site, you are expected to annotate the source from which it came. That is, you may use information on this site, but you must clearly state from where you obtained it. | ||||||||||||||||||||||
| Page last updated on |
||||||||||||||||||||||
Mac OS X Server Macintosh Matt's Hack TV
Software PS on Non-PS Printers Porsche
R E A D E R C O M M E N T S
Add a Comment