Computer Magic
Software Design Just For You
 
 

Special Webhosting Offer for Netbunch and Webhostplus customers!

June 7th, 2006

Special Web Hosting Offer!

As a former NetBunch (WebHostPlus) customer, I understand the frustration and heartache that you are going through. The considerable down time and effort spent just to get minor things fixed was one of the motivating factors for us to offer our own web hosting services. Since offering web hosting services, I have noticed an increase in the amount of traffic from others looking for help either dealing with this company, or looking for a way out. Here is your way out.

For all current or former NetBunch, WebHostPlus, Mesopia, and DR2.net customers who are tired of crappy, slow, and unavailable service, contact me directly at rayp@cmagic.biz and get a steep 50% discount on your first year of service on our already competitively priced packages. Have you or are you dealing with another web hosting company that had crappy service? Send us your story and we will extend the same offer to you! We will even assist you in moving your service and get you up in running ASAP (additional charges may apply if you require code changes or custom web applications/databases moved). No anonymous ticket system. A real person behind a real e-mail. Even a real phone number for business customers!

We are all busy and have better things to do than to spend hours and hours dealing with an unresponsive hosting company. Contact us and let us save you time and heartache. Let us manage your hosting experience with one point of contact and one bill. It is our mission to keep you from calling.

For more details on the packages we offer, visit our page at: Web Hosting

Remember to contact me directly to receive your 50% discount before paying.

Ray Pulsipher
rayp@cmagic.biz
Owner
Computer Magic And Software Design

PHP - Object References Vs. Object Copies (Pass by reference or pass by copy)

June 2nd, 2006

An important concept in programming is pointers. Even though we don’t directly use them in most modern languages the way we used to (think C/C++) they still come back to haunt us. In many modern languages they are referred to as references. References aren’t quite pointers, but they do resemble them enough to illustrate the concept (note that C/C++ has both pointers and references!).

What is the difference between a pointer and a reference? A pointer can be typed or non typed. A pointer can point to an arbitrary location in memory and invalid pointers (pointing to the wrong spot) is one of the causes of segmentation faults and blue screens in microsoft windows in the past. The pointer is very versatile, but also very dangerous.

A reference is similar to a pointer, but not quite. Think of a reference as a pointer to an existing varaible (not just some arbitrary pointer). See how it is similar? Some people get confused easily here.

In our case, we are only going to talk about references and how they work in PHP. References are important because they can affect the speed and functionality of a script. When a function in PHP is called, the parameters supplied are generally passed by copying the information. This means that if you change data passed into a function, when you leave the function the data will not have changed. Look at this example.


<?php

$a = “Testing…”;
print “Variable = $a…<BR>”;

function ChangeString($str) {
	$str = “String chanaged…”;
	print “>>> Local variable = $str…<BR>”;
}
ChangeString($a);

print “Variable = $a…<BR>”;

?>


This code shows the following output.


Variable = Testing……
>>> Local variable = String chanaged……
Variable = Testing……


Notice that the function modifies the string passed to it, but once we are done with the function and return to the main script, the varaible is untouched. This is because when we pass the variable to the function, it actually makes a new copy. This copy is also set so that is applicable only within the scope of the function (local scope). Some might think that this difference in scope is the cause of the copy. That is not the case. While scope is an important concept, it is not the reason the variable doesn’t get changed, the fact that it is copied is. Lets illustrate the point with some more examples.


<?php

$a = “Testing…”;
print “Variable = $a…<BR>”;

function ChangeString(&$str) {
	$str = “String chanaged…”;
	print “>>> Local variable = $str…<BR>”;
}
ChangeString($a);

print “Variable = $a…<BR>”;

?>

This is the SAME script, with just one minor change. In the definition of the function, we add the & character. This character means get a reference to the passed variable. In essense, we are now pointing to the original variable instead of making a copy. Here is the output now.


Variable = Testing……
>>> Local variable = String chanaged……
Variable = String chanaged……


Since we are now pointing to the original variable rather than making the copy, if we change either one, they both change since they both point to the same memory. Here is the same example without the function call. Here we will use simple variables to demonstrate the same issue. Note that both demonstrations (copying and referencing) are demonstrated in the same script; most of the script consists of print statements in order to show you whats going on.


<?php

print “The copy example…<BR>”;
$copy_a = “A value”;
print “>>> A = $copy_a…<BR>”;
$copy_b = $copy_a;
print “>>> B = $copy_b…<BR>”;
$copy_b = “B Value”;
print “>>>>> After changing copy_b…<BR>”;
print “>>> A = $copy_a…<BR>”;
print “>>> B = $copy_b…<BR>”;
print “<BR>———————————-<BR>”;
print “The reference example…<BR>”;
$ref_a = “A value”;
print “>>> A = $ref_a…<BR>”;
$ref_b =& $ref_a;
print “>>> B = $ref_b…<BR>”;
$ref_b = “B value”;
print “>>>>> After changing ref_b…<BR>”;
print “>>> A = $ref_a…<BR>”;
print “>>> B = $ref_b…<BR>”;

?>

The output looks like this:


The copy example…
>>> A = A value…
>>> B = A value…
>>>>> After changing copy_b…
>>> A = A value…
>>> B = B Value…

———————————-
The reference example…
>>> A = A value…
>>> B = A value…
>>>>> After changing ref_b…
>>> A = B value…
>>> B = B value…

When simply assigning a variable to another variable, you are actually copying that value. When you use the & sign, you are pointing the second variable to the first so that they both point to the same value or memory location. This means that changing one will change the other.

Some languages copy everything unless specifically told to reference. Others copy base data types (integer, float, string, etc..) and automatically reference object types (meaning you can assign variables to your objects like you are copying, but it will in the background reference it for you). Generally you just get used to how your language works and use it accordingly.

It looks like PHP changed how they do things between versions 4 and 5. In version 4, you have to explicitly reference everything if you don’t want to make copies. In PHP 5, it is smart enough to automatically pass references when you are working with objects. This created quite a headache when we tried to run a script written for PHP 5 on a server that had PHP 4 installed. The script had to be basically rewritten and lots of & symbols introduced. Why did PHP change things you ask? Because generally you want objects passed by reference. When adding the extra object oriented support in PHP 5, they decided to conform with most other languages in this respect. It was a good choice, especially since most people don’t have to worry about running new scripts on old servers. All old scripts that specifically use references should still work in the newer PHP versions.

Earlier I mentioned performance. Everytime you copy a value, a new chunk of memory has to be allocated. For base types (integers, etc..) this isn’t as big a deal. For Objects that may have lots of data, this can create a significant ammount of overhead. If you need a copy, then there is no getting around it, but some times you can spee things up quite a bit by using references.

On one project I was working on, a string variable held the whole contents of the current page (think 20-30K!). We sent this string to a processing function. At first, we used the normal method of passing in a copy, and then setting the string to the returned value:


$str = “lots of data”;
$str = ProcessString($str);

function ProcessString($s) {
	$s .= ” more data”;
	return $s;
}

This worked fine. The data from the $str variable was copied when passed to the function. The function would process the string, then pass back the processed string. Again, when passing or returning the resulting string, a copy is made again. So, our $str variable will hold a copy of a copy when we are done (or even a copy of a copy of a copy depending of if copies are made inside of the processing function!). You can see how this will add up.

By using a reference, you can modify the variable that is passed in directly. You ONLY want to do this when it is ok to modify the variable directly (not all the time!). By saving the copying where appropriate, you can speed up your code. The same code using a reference would look like the following. Notice that you don’t need to return a value now since the value being passed can be directly modified.


$str = “lots of data”;
ProcessString($str);

function ProcessString(&$s) {
	$s .= ” more data”;
//	return $s;
}

Ray Pulsipher

Owner

Computer Magic And Software Design

Sending email without all the hassle! Use the pickup directory.

May 26th, 2006

Ok, I just learned something new the other day. I have been using and administering IIS for years with or without the SMTP service running, but I never knew that you could send emails simply by placing them in the pickup directory. Yes, just copy a text file into the c:\inetpub\mailroot\pickup folder and the SMTP service will try and send it.

Why is that a big deal you ask? Ask those developers who have for years used packages like CDONTS. In some languages like PHP, the built in mail features are great and they just work. In ASP, the add-on libraries that you have to use leave a bit to be desired. Quite frankly, I like the idea of writing the whole email to a file and having it just head off into the blue.

How does this work you ask? I generally build up the email string first (easiest way is to send it to myself using outlook, then paste it into my scripts!), then write the whole thing to the directory when it is ready usinge fopen/fwrite. Chances are you will never actually see the file appear in your explorer view if the SMTP service is running as it will see the file and copy it to the queue right away. If it fails to send the email, it will dump the email to the badmail directory along with an error message in a seperate file (remove the boxes and you will see the error). If it doesn’t end up in badmail, it should be delivered shortly.

One thing to note is that we are working in a windows environment here. It is VITAL that you use the proper line feed characters (\r\n). Failure to do so (\n alone won’t cut it) will result in your email being rejected even if everything else is perfect. I kept getting an error code (0xC00402CE) for exactly this reason.

Here is an example of a simple email that is properly formatted.


Date: 27 Nov 2005 0852 GMT
To: “Bob Smith” <bobs@smith.com>
From: “Franky Rumple” <frankyr@rumple.com>
Subject: Testing Email
MIME-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable

Hello, this is email!


That is a basic email. If you create a text file with that information (put in your email address on the “To” line) and copy it to the pickup folder, you should get it in your inbox shortly. If you don’t, one place to check is the IIS Manager under the SMTP Service area. You can define the domains that it will forward mail to. In my case, I just add *.com, *.org, etc… This tells the SMTP service it is ok to send emails to people at those domains. You do want to be careful here as you could turn yourself into a spam relay if your SMTP service is unprotected. Mine happens to be behind a firewall, so it can only be used by internal applications to send email.

Using this method, it isn’t to tough to add attachments and such. You will have to format the email to use the correct mime headers and possibly use Base64 encoding on your attached files. This isn’t too big a deal though. To get the correct format, I suggest using your regular email client (Outlook?) and sending yourself an email with the attachment. Once it arrives, view the message source. You can copy/paste this and use it as a template to generate your own emails. If your attachement will change, then cut out that part but leave all the MIME headers. Then, with your code, plug in the appropriate data when needed (use str_replace and put a place holder, or concatenate as you go, whatever is easier). Make sure your data encoding matches the MIME header “Transfer-Encoding”. If it is set to Base64, then your data needs to be encoded using Base64 encoding.

If you use scripts, make sure to use \r\n for line feeds instead of just \n. Also, notice that after the headers (MIME or main Email headers) that you need an extra \r\n to seperate the header from the data. This is just a clue that the header is done and the message or data is beginning.

Generating emails is much easier than processing them. You can spend a lot of time learning about all the different headers and such and how to create them by scratch. While learning is always great, if you just want to get an email out, create one using Outlook and send it to yourself and use that as a template. It can really save you some time.

Ray Pulsipher

Owner

Computer Magic And Software Design

Working with Dynamic Connection Strings in VB.NET 2005 WinForms Applications

May 23rd, 2006

When Microsoft released their latest edition of Visual Studio 2005, they got a lot of things right. The data binding is finally ready for use in real projects, click once is a wonderful feature, and the support for connecting and editing data sources is great. One real problem with all this automation is that when something goes wrong, it really goes wrong. Do you scrap the automated features that can save you so much time? Hopefully this article will mean you wont have to when it comes to dynamic connection strings.

In the past, we used to define our connection strings to our database either right in the code or in a config file. Because we defined it and manually assigned it to the connection object, we could modify the connection string as required. It was a simple task to put a new server address in at run time or a new file path to an access database. The problem was that it was time consuming to re-write the connection code each time we needed to use it. There were several solutions that could ease our pain, but we were still doing things by hand.

When Microsoft added support for partial classes, they allowed much of the plumbing code to be seperated from the logic code. Now, for a form, there are 3 files involved. Much of the connection code is placed in the designer file where it is tucked away nicely. The problem is that this designer file is refactored (rewritten) when certain changes are made so you can’t gaurentee that any changes you make to the designer file will be there later. Manipulating the connection string here puts us back into manual mode and has to be done for each form/connection. What we really want is to just have our connection work and take advantage of the time saving features of drag and drop and data binding.

With all these great tools, we have lost control of the connection string. To really take advantage of the time saving tools in visual studio, we setup a connection in our server explorer. If that is the exact connection properties that will be in use when the project is live, then there is no issue. You can view tables, views, stored procedures, etc.. right from the server explorer. You can drop tables onto your forms and it will automatically setup connections, data adaptors, data sets, navigation bars, etc.. It makes setting up a basic data form extremely simple and fast. When you use a database server such as MS SQL or MySQL, you probly wont notice this issue until you decide to run your app off of two different servers (one for development and one for production). If you use MS Acces for your database, then you are probly reading this because you already have the problem.

With MS Access, your connection string includes the path to your MDB file. Lets say for this example that the MDB file is in your project directory. You could use a full path(c:\projects\myproject\data.mdb) which would work fine while you are developing your application. What about when you send the app off for your friends or coworkers to use? The chances that your program will be in the exact same folder as on your computer can be slim and you don’t want to have to recompile your app each time some one runs it in a different directory (if you even knew what that directory was!).

The easy solution is to use a relative path. In this case, you could just specify the MDB file name without any path. This works great in the IDE when you are dragging and dropping things, but as soon as you push the play button, you compile and create either a debug or release version of your app. This app is then placed in the bin/debug or bin/release directory. Suddenly your MDB file is no longer in the current directory (it is two levels back in the project directory) and your app can’t find it.

Shucks, I will just add my MDB file to the project, then when it is built, it will copy the MDB file over to the bin/debug directory. This actually works, except that each time you build your MDB file will be copied over again meaning that any data you enter while the program is running will be over written (this actually had me confused for hours and I though changes werent sticking and I was trying to debug my save code). You can set the properties of the MDB file in the project to “Copy if newer” which will help, but if you make changes to the main MDB file, then it will copy over the on in the bin/debug folder.

It would be nice for the connection string to be smart enough to use the project folder as the main folder all the time. You can actually mess with this by having your builds reside in the project folder. This almost solves most of your problems. This solution clutters up your folder and while it might work for certain instances, it isn’t the best solution.

Now that we have address most of the issues you come across while developing and testing your software, lets talk about issues you face when you deploy it.

In my case, I had yet another problem. When deployed, the user could pick from more than one database. They had the option of browsing to an MDB file and choosing the current data to work with. How do you put in this new data path without writing custom code in every single form or location where you use a connection string without messing up the configuration while you are in the development environment?

First, go to “My Project” and go to the Settings tab. Click the View Code button on top. Add the event “Settings Loaded”.

This event fires after the settings have been loaded. When you setup connections in your server explorer, it adds connection strings to your settings area. These strings are specifically marked as a connection string. What we can do here is loop through our settings and find the connection strings. This happens early in the application and allows us to set the connection string before they are even utilized by the connection objects in the application. By changing them here, the rest of the application can just work without knowing that anything changed.

You could specifically change one connection string instead of looping, but I found that as I worked on my project that the IDE liked to add CString1, CString2, CString3, and so on. A new string (identical to the first) was added every time the project needed to reconnect to the database. Simply changing the connection string name or deleting it causes problems with data sets later on as the data sets specifically reference the connection strings by name. The easiest method for me was just to update all connection strings to point to the new location.

The advantage here is that the changes can be made in one location and you can put in the code to handle running in the IDE or deployed.

Here is the actual code:


Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
            ‘ Find the current file…

            If (Debugger.IsAttached) Then
                ‘ Don’t mess with the path if we are running in the IDE
                ValidConnection = True
                Return
            End If

            ‘ Grab the last MDB file we opened and try to get it
            Dim fname As String
            fname = My.Settings.LastDBFile

            ‘ Try and load the file..
            ‘ CheckDataFile checks to see if the file exists…
            ‘ UpdateCStrings modifies all the connection strings to use the new file name
            If (CheckDataFile(fname)) Then
                ‘ Good file.. lets use it…
                UpdateCStrings(fname)
                ValidConnection = True
                Return
            End If

            ‘ If the last db file cannot be found, start looking in the current directory, then start looking in parent directories
            ‘ Load the correct database directory

            Dim data_directory As String = “”
            data_directory = Windows.Forms.Application.StartupPath
            data_directory = data_directory.Replace(”/”, “”)
            If (Right(data_directory, 1) <> “”) Then
                data_directory += “”
            End If

            Dim p_dir As String
            p_dir = System.IO.Directory.GetParent(data_directory).Parent.Parent.FullName + “”

            ‘ Use this in case your MDB file isn’t copied to your bin/debug folder when you push play
            ‘ Look for: dbfile, ..\..\dbfile
            Dim last_db_file As String = “”
            last_db_file = My.Settings.LastDBFile

            ‘ Check the current directory
            If (CheckDataFile(data_directory + last_db_file)) Then
                UpdateCStrings(data_directory + last_db_file)
                ValidConnection = True
                Return
            End If

            ‘ Check the parent directory (../../dbfile)
            If (CheckDataFile(p_dir + last_db_file)) Then
                UpdateCStrings(p_dir + last_db_file)
                ValidConnection = True
                Return
            End If

            ‘ Those others didn’t work, lets try other common locations.
            ‘ Try to open from my documents…
            data_directory = My.Computer.FileSystem.SpecialDirectories.MyDocuments.TrimEnd(”") + “”
            If (CheckDataFile(data_directory + last_db_file)) Then
                UpdateCStrings(p_dir + last_db_file)
                ValidConnection = True
                Return
            End If

            ‘ Couldn’t open any db file
           ‘ Show some kind of error….
        End Sub

The code is really long because it accounts for several different options. First, if we are in the debugger (IDE or playing from the IDE) it doesn’t do anything as the connection strings should work fine already from that location. Next, it tries the currently saved file path (we save the last used file path as a user setting). Next, it tries looking in the current directory, the parent directory (../../dbfile.mdb) to account for your application running in the bin/debug folder. Next it tries to load the db file from the MyDocuments folder.

You can adjust your code any way you like so that it works for you. The CheckDataFile function just uses the File.Exists method to see if the MDB file exists. In our case, if it does exist, it also tries to load it and check a version. That way we can update the MDB file if changes were made to the structure when it is loaded (eg: I add a new table to my MDB file while developing, so I can update the clients MDB file also when it is loaded). You can do whatever you want with the CheckDataFile function. It just takes the current path and checks things out, then returns a true or false based on its ability to find and load the file. I won’t list it here since it is mostly very specific to my project.

The next function that is important is UpdateCStrings. This takes the new file path and loops through all the connection string items in your settings updating them to point to the new path. I will list that here:


Public Sub UpdateCStrings(ByVal fname As String)
        Dim item As System.Configuration.SettingsProperty
        ‘ Loop through each property (each setting)…
        For Each item In My.Settings.Properties
            If (item.PropertyType Is GetType(String)) Then
                Dim keys As System.Collections.ICollection
                keys = item.Attributes.Keys
                Dim attr As System.Configuration.SpecialSettingAttribute
                Dim key As Object
                ‘ Each property has multiple attributes, we are looking for the connectionstring special attribute
                For Each key In keys
                    If (item.Attributes.Item(key).GetType() Is GetType(System.Configuration.SpecialSettingAttribute)) Then
                        attr = item.Attributes.Item(key)
                        If (attr.SpecialSetting = Configuration.SpecialSetting.ConnectionString) Then
                            Dim tmp As String
                            ‘ Found a connection string… convert the connection string…
                            tmp = ConvertConnectionString(fname, item.DefaultValue)
                            ‘ Assign the new connection string  back to the setting
                            My.Settings.Item(item.Name) = tmp
                        End If
                    End If
                Next

            End If

        Next
    End Sub

Public Function ConvertConnectionString(ByVal fname As String, Optional ByVal cstring As String = “”) As String
        If (cstring = “”) Then
            cstring = My.Settings.MyConnectionString
        End If
        If (fname <> “”) Then
            Return cstring.Replace(”|DataDirectory|\mydb.data”, fname)
        End If
        Return cstring
    End Function

The first function does the looping and is responsible for identifying which settings are connection strings. Once it finds one, it sends the item to the ConvertConnectionString to change the path in the cstring. One thing that you may notices is that Visual Studio likes to use |DataDirectory| for your file path if the MDB file is in a specific locaiton. The mydata.mdb file is the name of the file I used during development. Thus, all connection strings should point there to start with. The easiest method is to simply replace that with the new path. Note that we call the ConvertConnectionString function using the DefalutValue rather than the current connection string as it could have already been modified and the |DataDirectory| value may no longer be present. You could use fancier parsing techniques, but I have found that the simple string replace method works great for me.

The hardest part by far is the looping through the properties to identify which settings are actually connection strings. There is little real documentation on this and I had to hack it out by hand. Hopefully this will save others a great deal of time. I know that on other projects, being able to drop this in will cut of many hours and allow me to still take full advantage of the built in drag and drop databinding features in Visual Studio .NET 2005.

In our app, we use the UpdateCStrings function later on. The user can browse for a new DB file at any time and the UpdateCStrings function can be called using the new supplied path. The settings event helps to make sure that the connection strings are all set as the application starts.

Ray Pulsipher

Owner

Computer Magic And Software Design

XML Demystified…

May 11th, 2006

I don’t know how many times I hear the question, “I should learn XML so my web pages can be standards compliant.” Huh? XML? Oh, you mean XHTML? Yeah, you should learn that.

There is a whole group of technologies involved when you start talking about XML. There are also so many products out there that advertise its usage. It has become cool to work with XML. In computer land, it is normal for an acronym or term to catch on and be used out of context by those who don’t know better, and even by those who should know better.

Case in point. Every one wants to be on the web, but most people think of the web as what you see in your browser. They don’t understand that the web is really a combination of all the interconnected computers and is protocol indipendant. It has gotten so that when you say, “let’s surf the web”, every one (including techs) assume you mean to open up a browser and use the HTTP protocol to download web pages. While HTTP traffic accounts for a significat part of the web, it is by no means the only traffic flowing through the web. Web traffic includes email, FTP, SNMP, File Sharing, Telnet, SSH, Instant Messengers, online TV/Movies or Radio stations, and the list goes on and on.

When referring to XML, there is the same type of misunderstanding taking place, even among those who should know better. We will try and break down the seperate components that we refer to as XML and show you the differences.

First, XML is a thing all by itself. It isn’t really that exciting. It actually has a number of weekneses, and is kind of wordy which increases your data size (some times significantly). It is a text based specification and requries encoding (base 64 encoding is popular) for binary data which can again increase data size (base 64 ads one third to the size), and it requires a nice XML library to really utilize it in your software (unless you want to rewrite the code to parse and manipulate XML yourself).

Why would you use it then? The main reason is that it allows interoperability in a generic manor. It does this specifically because many ports of the XML processing library have been done. You can use XML in just about any language by plugging in free libraries. This is an important concept. People have been using CSV files for years, but you don’t find much support from language companies for this format. Most languages already have an XML library ready to go or you can get one as an add-on.

One it’s weaknesses is also it’s strengths. It is wordy. If done right, it is often self describing. It makes it easy for developers to simply read your XML file and to find ways to integrage with it.

With this little bit of background, lets actually answer the question, “What is XML?”

XML is a generic way to store data in a self describing, orderly, hierarchal manor. More importantly, people started getting together and promoting a standard which every one else has done a good job of following. This standard is what makes it so people can write software that interacts in an easy and platform independant way. This in and of itself isn’t revolutionary. People have been writing software that interacts with other software for years (web servers and web browsers). The problem with other formats is that they were too specific in their features (why do you think we still use FTP when we have web pages?) or they were too difficult for other developers figure out what went where (try parsing an ms word document even with the complete file specifications). The amazing part is that the industry got together and agreed on something and actually started supporting and using it. Now you can have java web services supplying information to .NET driven applications.

XML itself is just a generic way to store data (store meaning transfer to other software or save as a file, or manipulate in any way you need). Below is a small sample.


<ROOT>
  <PERSON name=”bob” age=”84″ height=”5.0″>
    <OPERATION date=”5/5/05″ type=”heart transplant” />
  </PERSON>

  <PERSON name=”joy” age=”13″ height=”5.4″>
    <OPERATION date=”1/1/06″ type=”hip fracture” />
    <OPERATION date=”1/3/06″ type=”mole removal” />
  </PERSON>

</ROOT>

The example shows how you can stack related information on people (or patients in this case). Each person gets his/her own tag. Notice that the tag opens “PERSON” and closes “/PERSON”. EVERY TAG has to open and close. This is a requirement in XML. NO EXCEPTIONS! Really. None.. Not even the OPERATION tag… Notice at the end, it has a />. The / character at the end is a short cut to close the tag. It is important to note that you can’t use the short and long hand versions at the same time.

You would use the long hand version when you have more data to nest in your current item (as one or more operations that belong to a particular person). If the current item has no child items, then you can use the short hand version (or you could use the long hand version).

Another thing about XML is it HAS to have a single ROOT element. The ROOT element can be called anything you want, but there has to be one. Why? I don’t know, look it up and get back to me. Guess it just likes a single starting point in its hierarchy.

Now that you have seen a basic XML file, you will notice that it is really just a data dump that has been formatted in a generic way. Any XML parser can now read that in and you can grab values at each node. It looks very much like HTML, but it IS NOT! There are a few rules that come with XML, and one is that all XML is well formed. This is an important concept and relates to the requirement above that each tag is closed properly and in the correct order. This is what we call well formed XML code.

When you hear about XHTML, one of the main differences between HTML and XHTML is that the XHTML be well formed also. It was common pracitce to put some tags in without a closing tag (P, BR, IMG, etc..). In XHTML, you HAVE to close these tags. You can use the short or long hand method where appropriate. There are many other enhancements that came along at the same time as the XHTML spec came out, and those really have nothing to do with XML itself. Ehancements to the DOM object model made working with page and content of that page more uniform, but most of the actual XML related features most HTML designers don’t even use.

Now XML itself is just data formatted in a generic way. There are some other tools in available that can take XML and do stuff with it. XSL is basically a style sheet for XML files. Looking at an XML file itself is boring and technical. You wouldn’t want to display results on your website using just XML. You would wrap it with rendering instructions in an XSL file that would apply styles and HTML tags around the XML data and make it into a presentable page. This doesn’t save you from doing HTML, but it can save you from having to write some programming code. XSL can sort, select individual or multiple records and format the display of the data, all with no programming. Will it take the place of languages like PHP? No way. The XML still needs to be generated some how. It could be used in conjunction with current technologies though.

XML is used in many different places, for data storage, for interprocess communications, for impressing your friends, but where is XSL used? There are actually a suite of technologies that go along with XSL including XPath (the part that allows you to query and sort XML files in an SQL like syntax). Generally when you move away from the web, you aren’t worried about the display of the XML file as much as you are worried about having the software on the other end be able to understand what you have sent. Here is an example where we used XML to make a project communicate with the server.

One client of mine has a server where data is stored and entered on a daily basis. This data is manipulated through a web page interface. This works great. At times, they need to go on the road and gather results in places where internet access is limited. We wrote an applicaiton that allows you to download the needed information from the server and take it with you. This information comes down in the form of an XML document generated by a PHP file. The PHP file adds the neede XML tags around the data and includes extra attributes that affect the display properties.

The application itself opens the XML file using the microsoft XML library which parses the XML file for us. We can then ask for the nodes we want andchange them as information is manipulated. The application also uses the extra information in the attributes that can tell it which menu to show when they right click a record, or to hide records in the GUI, or a number of other features. The nice thing about the XML output is that it is generic and extensible which allows us to ad attributes and elements as we go without it breaking everything. We don’t have to worry as much about file version (ever try and open up a word 2003 document in word 97?). As updates are released for the application, we can modify the base XML file without the program blowing up.

After hearing all this great stuff about XML, it is kind of an anti-climax to hear what XML really is. It is just a generic data container. The fact that every one in the world seems to have adopted it is what makes it so great. I can write a service that uses XML to communicate and it is easy for others to write software that can interact with my software without worriyg about languge or operating system

One area where XML is getting a lot of use is in the Ajax libraries. There is plenty of descriptions of Ajax and what it does (including on this website), but not all of them explain how XML fits in. In our previous example, we use XML to transfer database information to the applicaiton in a generic way that can be parsed and understood. Ajax does the same thing. When you add Ajax to a website, you make it behave more like an application. In doing so, you still need to make requests to the web server. These requests are sent via HTTP, but the requests still need to be understood by both parties. HTTP is like the telephone in this case and XML is like the conversation. The XML that comes back from the server can be interperated in the background and used to populate information on the web page in realtime. The XML is again, just a generic way to do this as often times the XML returned is customized for the particular application and instance. They really could have used any file format (CSV, etc..) but XML was chosen because of its generic intreface and for JavaScripts built in ability to parse and work with XML.

If you want all the other bells and whistles associated with XML, you should look further into XSL to see how easy it is to manipulate XML without being a programmer. We are actually using XSL to format a data driven printout. If I ever get done with this articile, I am off to finish it.

In any attempt to get people to adopt your product, you have to remove the barriers to entry so that it is as easy as possible. XML libraries are available for all langauges and platforms and most often now included in development tools. Browsers have built in support for XML. People have been using XML forever now and haven’t even realized it. They have done a good job of defining XML (at least for the programmers) and making it easy to use. The marketing has also been particularly good at making people aware. Even though many people don’t totally understand what it is, they know they want it. I have used it in several projects to date and have found that it can save me significant time because the libraries are so avialable and they do what we would have to do manually with our own file formats. This alone saves me enough time to adopt XML in my projects.

Ray Pulsipher

Owner

Computer Magic And Software Design


Home | My Blog | Products | Edumed | JParks Design | About Us | Portfolio | Services | Location | Contact Us | Embedded Python | College Courses | Quick Scan | Web Spy | EZ Auction | Web Hosting
This page has been viewed 113593 times.

Copyright © 2005 Computer Magic And Software Design
(360) 417-6844
computermagic@hotmail.com
computer magic