Wednesday, December 12, 2007

[Javascript] XML Loading

Problem: writing down a custom visual XML editor (based upon a specific and complex XSD schema definition), the first task was parsing XML string / remote XML file into a DOM compliant document object.



Solution: the first task is to recognize Explorer and Mozilla browser compliance. This can be surprisely done by checking browser's capability to handle XML content, that is to say wich objects the browser can use.
To achieve this target, I used those global variables:

var isIE = (window.ActiveXObject)?true:false;
var isMozilla = (document.implementation.createDocument)?true:false;

That means if you can instantiate an ActiveXObject, you are Explorer, while if you can execute che createDocument method you certanly are Mozilla compliant.
I have no documentation of the reason of those differences, exception made by the fact that Explorer fully operates with ActiveX objects).

Next step depends on the way you want to open XML data: from a file or from a string.
In the first case this is the code:

var xmlDoc  = null ;
function importXML(url)
{

if (isMozilla)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.onload = onContentLoad;
}
else if (isIE)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) onContentLoad()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load(url);
}
function onContentLoad()
{
/* handle the loaded content */
}
As you can see I used a global variable "xmlDoc" wich is the DOM document: this is correct in a procedural coding style, but in my opinion would be better to put it into OOP style (so "xmlDoc" becomes a member of the class).
The function simply load different object is case of IE or Mozilla, and as the file is supposed to be in another domain - it can take several time to load - after the loading is called the "onContentLoad" function, in wich you do wathever you want to inizialize your application.

I used this method to load some XML templates or create a new XML document, useful to create new fragment of the defined document (insted of inserting XML logic into javascript code, is better to load a specific fragment, so if your schema definition changes, your javascript script is still effective).

I use a Java Servlet in my application, that aswers HTTP requests: one of this requests is a "loadSchema" request, with wich I receive in a string an entire XML document (wich itself describes the schema - not a XSD schema - of a specific application behavior): so the problem is to parse a string into an XML document.
This is the code:

function loadXML(xmlString)
{
if (isIE)
{
this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
this.xmlDoc.loadXML(xmlString);
}

else if (this.isMozilla) {
var domParser = new DOMParser();
this.xmlDoc = domParser.parseFromString(xmlString,"application/xml");
}

if(
xmlDoc==null)
alert("Error.");
}

Code is quite simple, so no explanation is needed.

Ending, this is the code to handle a "GET" HTTP request via Javascript (off course there can be many ways to customize this code, there are several options, but guys...this is butchering!):

var xmlHttp = null;

function requestSchema(url)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
xmlHttp.onreadystatechange=stateChangedRequestSchema;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChangedRequestSchema()
{
if (xmlHttp.readyState==4 xmlHttp.readyState=="complete")
loadXML(xmlHttp.responseText);
}

The "requestSchema" simply makes the GET request, and delegates "stateChangedRequestSchema" to handle the load event (calls "loadXML" passing the content received by the request, that is supposed to be a valid XML document....but you have to write down the code to check if it is correct!).

Once loaded the content in the DOM object, you can use all DOM compliance methods to navigate into your document and do wathever you want. A good and quick reference to javascritp DOM reference can be found here.

You should not be surprised or highly shocked if handling XML content is so simple: HTML is an XML based language itself, so it would have been fucking idiot not to support natively XML parsing.

Bye Bye, hope to help you!

Friday, December 7, 2007

[VS2005] Project Template(s) Missing

Problem: All of a sudden you notice all or some of you Visual Studio 2005 Project Templates are missing.

Solution(s):

  1. run "devenv /InstallVSTemplates" from Visual Studio command prompt
  2. if the above didn't work, log out and log in as administrator then try again solution 1
  3. if all the above didn't help you , this will do it: Open Visual Studio, in the menu, Open Tools->Options->Projects and Solutions->General. At this point you will notice the path of Project Templates is set to "C:\Documents and Settings\[yourUserName]\My Documents\Visual Studio 8\Templates\ProjectTemplates" or something very similar to that. In order to fix it you gotta set that path to C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Project Templates or the same path according to wherever you installed Visual Studio.

This extremely annoying thing happened to me twice on different systems, and I think is somehow related to the installation of earlier Ajax.NET VS2005 extensions that somehow is messing everything up (bloody butchers). If you're able to spot Viz template directories, your first temptation would certainly be to copy all that zipped stuff over from the installation folders to the other Document and Settings path: just don't do that, it will solve nothing and you'll eventually end up losing all your templates. This 'cause the templates installation process involves more than just copying the stuff over to another directory. I was able to fix it using methods 1/2 on a system, but not on the other. Notice that with method 3 you are detaching Project Templates from your user account, but if it doesn't bother you and you don't know how or you just don't wanna bother messing with windows accounts, then this is your way to go.