XML and PHP Simplified - The Createdoc.php Script / Page 2 | WebReference

XML and PHP Simplified - The Createdoc.php Script / Page 2


[prev]

XML and PHP Simplified - The Createdoc.php Script [con't]

The editor script explained

We've only discussed the code fairly briefly so it warrants a more in-depth look. Then we will look at the last remaining script of the application. The code starts by opening a session; all scripts in this application will start by opening a session because the file that is going to be used is located or stored in a session variable:

After declaring the HTML headers we create a HTML table that will hold all the information about the editors output:


The first part of the editor parses the chosen XML document into its hierarchical representation. Now I've used the term 'hierarchical representation' quite a few times so let me explain what I mean by it. When you convert an XML document into an object through the DOM or simplexml extension, you put each element into an organized system. This system labels the first element tag as zero(0) then the next as one and so on. That way if you want to access a particular element, you simply identify it by that number and then you access that particular tag's textual representation. The very top of the table displays the name of the file that is currently loaded in the editor:


Before the document name is shown, we need to remove the path information from the filename. Otherwise we will have an ugly and long pathname preceding the filename that looks something like this:

/Inetpub/wwwroot/XMLPHP/XMLdocviewer/docs/myxml.xml";

Instead of something neat like this:

myxml.xml

To remove the path information, we use a function called pathinfo(). This function returns path information about a file. The advantage here is that it can be broken down into directory name, file, basename and extension. What we want is the base name part since it will contain the full name of the file. So we load the name of our xml file into the pathinfo() function:

$removepath = pathinfo($_SESSION['file']);

Then we remove the path information and only show the full filename:

echo "".$removepath['basename']."";
}else{ 

If the XML document filename is not set then the user did not select an XML document to work on; we redirect them to the main page of the application:


Now that we have the name of the XML document 'cleaned', lets move on and parse it. Before we do that, we establish if the document is saved in the session variable and if it exists:


Then we load the file into an object:

$xml = simplexml_load_file($_SESSION['file']);

Then we simply parse it:


We show error messages if the file is not found or if the user did not select a file to work on:

The second function of the editor is to show the tags of the XML document, to do this we have to load the document into a DOM object first:


Then we verify that the XML file has been loaded; if not we show an error message:


Then we import the DOM object into a string using the simplexml_import_dom() function:

$s = simplexml_import_dom($dom);

We now need to display the document tags into HTML form fields:


To load the tags into the form fields we run a loop. The loop will iterate through the hierarchical listing and display each tag's information in separate form field units:


Each unit will have a form button that will be used to submit the tag information that you want to change.

The changetags.php script

This script is responsible for processing the information that we send from the editor. More specifically, it enables us to change specific information in a given tag. Below is its code:


The code for this script is relatively straightforward. It starts by opening a session:


Then we check to see if the form has been submitted; this time we use the hidden form field as the flag:


Since all the form fields are required, we need to make sure that none of them are empty:


Since we are expecting a mix of form variables, we need to be sure that they are of the right type. For example, the arrval needs to be of type integer, because it is going to ID the tag that we want to change. We use the is_numeric() function to differentiate between string and integer:


If the $msg variable is empty then we continue to execute the code:


We create a new DOM object:


Then we load the XML file:


Then we load the child elements:


Now that we have the contact tag loaded, we select the element that we want to replace using the arrval form variable to identify and select it:


Then we set the new value that we want to use:


Finally, we replace the value:


Then we save the file using the original name and then go back to the editor:


If any errors occurred, we show them here:


I hope that the series of articles have made it a little easier to work with XML documents and that it also shed some light on how to use the DOM. Feel free to modify the program to suit.

Download the files for this series.

Original: September 23, 2009


[prev]