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

XML and PHP Simplified - The Createdoc.php Script

By J. Leidago Noabeb


[next]

In this article we will continue to look at the create document script, focusing on the HTML portion of the code. Then we will continue to look at the document editor script, which is at the heart of the program.

The createdoc.php script continued...

The HTML portion of the page contains an HTML form with over five input elements. It is mainly responsible for collecting information that will eventually be used to formulate the XML document. It collects the following information:

  • XML document name – The user does not have to include the xml extension; it will automatically be appended to any name that the user gives.
  • Root element name
  • Name of contact
  • Email address of contact
  • Address of the contact

The above information is all that is required from the user. With this information, as you've seen in the previous article, the program will make up a well formed XML document. So lets look at the HTML.

First, we set the page headers; note that we use a template for this and other scripts:



If any errors have been detected during the data validation process, it is shown here:


This row presents an input field that takes the XML document name that you want to give to your document. A pre-filled in name of xmldoc is given here:



Second, the root element name is required:



The rest of the code presents input fields for the remaining child elements:



Finally, we close the form and HTML headers:


The editor script

The editor script is responsible for breaking down a selected XML document to its bare structures and presenting it to the user in an ordered manner. For example, the XML document called myxml.xml looks like this when presented:



When broken down to its internal hierarchical representation this is what it looks like:



The user is presented with something similar; in addition, the user also has the opportunity to change any of the tags that they want to change. The document elements are presented in an HTML form that is pre-filled with the names of the tags of the document that is loaded in the editor. The user can then easily modify those names and send the changes to a processing script. The script uses the simplexml extension as well as the PHP DOM extension. For example, to parse an XML document into its hierarchical representation it uses the simplexml_load_file() function that loads an entire XML file and turns it into a object. Once the document is converted into an object, you can pretty much manipulate it like any other object. You can access its properties and attributes just as easily as you would an ordinary object. It also uses both the DOM and simplexml extensions together in the form of the simplexml_import_dom() function. This function takes a DOM object and turns it into a string for easy manipulation. The modified XML document is then saved in its original name. Below is an image of what the script looks like in action:

Figure 1

The code that does all of this is listed below:



The code above uses both the DOM and simplexml extensions to achieve its functionality. So let's take a look at the code.


[next]