Building a CMS, part 2: Taking it to the server - Generating the XML
(Page 2 of 5 )
There is one more thing we need to do before we send our form data to the server. Each company description needs XML markup added to it. Alternatively, we could generate new hidden form fields for each description or use some sort of delimiters to separate each description in a single hidden field; we could then add all XML tags at the server. It doesn't take much, however, to just add the XML we need using Javascript just before we send it all to the server.
function subToServer()
{
var form = document.forms.release;//get the form object
var descField = form.desc;//get the hidden form field for sending descriptions
var descStr = "";
if(descriptionObj)//if the user did some editing
{
var len = descriptionObj.desc_Arr.length;//how many do we have
for(var i = 0;i < len;i++)
{
descStr += "<header>" + descriptionObj.head_Arr[i] + "</header>\n";
descStr += "<description>" + descriptionObj.desc_Arr[i] + "</description>\n";
}
}
else// if no editing was done use defaults
{
descriptionObj = new description(null,null);
descStr += "<header>" + descriptionObj.descHead + "</header>\n" +
"<description>" + descriptionObj.descText + "</description>\n";
}
descField.value = descStr;//put the data in the hidden field
return true;//send it to the server
}
This function is called by the onSubmit event of the form. Returning true gives the go ahead to post the form.
<form action="dxml.php"
method="post"
enctype="multipart/form-data"
name="release"
onSubmit="return subToServer()">
Taking it to the Server
Now we move to the server side PHP code. First let's set up an associative array that contains the markup for each of the features of the document. Since we are dealing with XML, we will need the required XML declaration at the top.
$xml_arr["doctop"] = "<?xml version=\"1.0\"?".">\n".
"<?xml-stylesheet href=\"release.css\" type=\"text/css\"?>\n";
One important thing to note about using PHP with XML and XHTML documents is that, because XML declarations use a "<? ...... ?>" syntax, you will need to echo these lines, instead of leaving them in the document as markup. If the PHP interpreter hits this markup it will produce a parse error.
You will also notice in the above example that we have a CSS style sheet declaration. Current XML aware browsers allow you to use CSS to style XML elements. There is no link or style element in XML, so instead you use the xml-stylesheet declaration. Serving our XML press release in CSS styled XML isn't the best idea if we want all browsers to render it properly, but it serves us well for testing purposes before we add the XSL transformation code.
In styling XML elements it's important to realize that the browser knows nothing about them. This means that you will be using the CSS display property for each element to indicate to the browser whether an element is to be displayed as a block, inline or whatever other values are supported by the browser you are using. Some browsers also require a DTD (document type definition) to render XML.
Next: Creating the Markup >>
More Web Hosting How-Tos Articles
More By Chris Root