Main Page

Modifying DOM creation

Unfortunately, the only way to get specific error information is to parse the error message text. The easi-
est way to do this is to use a regular expression:
var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column
(\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/;
This rather long regular expression pulls all the relevant information out of the XML code. The first captur-
ing group retrieves the error message, the second retrieves the filename, the third retrieves the line number,
the fourth retrieves the column number, and the fifth retrieves the source code that caused the error (with-
out the trailing dashes and caret). To use this to create an error message, just use the
test()
method:
var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column
(\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/;
if (oXmlDom.documentElement.tagName == “parsererror”) {
reError.test(oXmlDom.xml);
alert(“An error occurred:\nDescription: “
+ RegExp.$1 + “\n”
+ “File: “ + RegExp.$2 + “\n”
+ “Line: “ + RegExp.$3 + “\n”
+ “Line Pos: “ + RegExp.$4 + “\n”
+ “Source: “ + RegExp.$5);
}
Making interfaces play together
Developing with the XML DOM is only useful if you have a cross-browser solution. As you can see, the
IE and Mozilla implementations differ enough to cause significant problems for you when you are
developing. The only solution is to come up with a common way to use the XML DOM that works in
both browsers.
Modifying DOM creation
The first step is to create a common way for IE and Mozilla to create an XML DOM object. The easiest
way to do this is to create a pseudo-class that enables you to create an XML DOM like this:
var oXmlDom = new XmlDom();
Of course, to make this work, you need browser detection going on inside the constructor for XmlDom:
function XmlDom() {
if (window.ActiveXObject) {
//IE-specific code
} else if (document.implementation && document.implementation.createDocument) {
//DOM-specific code
} else {
throw new Error(“Your browser doesn’t support an XML DOM object.”);
}
}
455
XML in JavaScript
18_579088 ch15.qxd 3/28/05 11:42 AM Page 455


JavaScript EditorFree JavaScript Editor     Ajax Editor


©