XML and PHP Simplified - Building an XML Reader | WebReference

XML and PHP Simplified - Building an XML Reader

By Leidago Noabeb


[next]

Building an XML Reader

In this article, we are going to build an application that will enable us to work with XML documents. Since we've covered quite a bit in the last couple of articles, we are going to use that knowledge to create this program. We've already looked at the program requirements in the previous article. To refresh our memories, I will provide its code here so we can work through it. The first page in the program is where we will select the XML document that we want to work with. Alternatively, you can choose to create your own XML document to work with. The page provides a link for it. The page has the following look and code:

Figure 1:Main Script

and the HTML of the script:

Let's take a look at the code of the page. We immediately open a session. This is because we are going to store the name of the selected XML document in a session variable so that it is available to any page that will need it:

Then we check to see if the form has been submitted:

We then set some variables:

$msg="";

We also set the default directory, where our XML documents are located, to make it easy for the program to simply select and process the XML documents that you want to work with:

$default_dir = "/Inetpub/wwwroot/XMLPHP/XMLdocviewer/docs";

We make a second check, which is very important at this stage, to ensure that the user has selected something (and not just pressed the submit button) and that the selected name is not set to "none":

Next, we transfer the document name to a shorter more identifiable variable:

Now we check if we can access the default directory. It could be that the directory does not have the appropriate permissions; in this case it is simply a matter of going into the properties of the folder in which the XML documents are located and granting access to this program. To make things easier, it would be better to give full access rights to a user called ‘everyone' in Windows. Simply right click on the folder and then select properties. Then select the ‘Security' tab and click on ‘Add'. Then click on ‘Advanced' and then ‘Find Now'. Select everyone and then OK twice. Then give full control to this user. That will sort out any problems that you may encounter. These instructions are for the Windows users:

If not, we show an error msg:

         die ("Cannot open" .$default_dir);
      }else{

Otherwise we append the filename to the default directory:

Once we are sure that the user has selected a file to work with, we put the name of the file in a session and redirect the user to the document editor script:

If the user does not select a file name then we simply display an error message:

First, we set the HTML headers:

We are using a template for all our pages and its definition starts here:

We then create the table that is going to host our dropdown box:

Now we get to the heart of the script. This is where we will set up the dropdown box with a list of names of all the files in the directory:

First, we set the default directory. This directory contains all the XML documents:

Then we make sure that the directory actually exists and that it can be accessed, before trying to use it:

Then we run a while loop to read the files in the directory; we only want files not directories or folders so we put a restriction on it:

and then display the file names:

We then free the resources that we've been using and close the select box:

If the user does not want to work with any of the files listed in the select box, then the user has an option to create their own XML document:

That's it for the main page.


[next]