AJAX and XPATH

M

Mariano

Then i have read an example on a book that explain how to read XML
node and return answer with an Ajax approach.
This is the js script:
_____________________________________
function checkUser() {
var mozillaFlag = false;
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
XMLHttpRequestObject.overrideMimeType("text/xml");
mozillaFlag = true;
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new
ActiveXObject("Microsoft.XMLHTTP");
}
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open("GET", "xml/utenti.jsp", true);
XMLHttpRequestObject.onreadystatechange = function() {
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
var xmlDocument = XMLHttpRequestObject.responseXML;
if(mozillaFlag){
removeWhitespace(xmlDocument);
}
displayGuest(xmlDocument);
}
}
XMLHttpRequestObject.send(null);
}
}

function displayGuest (xmldoc) {
var displayText;

var utenti = xmldoc.documentElement;
var utente = utenti.firstChild;
var nodiUtente = utente.attributes;
var user = nodiUtente.getNamedItem("username");

displayText = "this is the username: "+user.nodeValue;

var target = document.getElementById("usr_prob");
target.innerHTML=displayText;
}

function removeWhitespace(xml) {
var loopIndex;
for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex+
+) {
var currentNode = xml.childNodes[loopIndex];
if (currentNode.nodeType == 1) {
removeWhitespace(currentNode);
}
if (((/^\s+$/.test(currentNode.nodeValue))) &&
(currentNode.nodeType == 3)) {
xml.removeChild(xml.childNodes[loopIndex--]);
}
}
}
_____________________________________
That's is fully work, if I recall this function in HTML file, it will
be correctly modified where the id tag usr_prob is present (ajax is
cool).

The only problem is, the using of XML document:

var utenti = xmldoc.documentElement;
var utente = utenti.firstChild;
var nodiUtente = utente.attributes;
var user = nodiUtente.getNamedItem("username");

this way is too "expansive". I would use XPATH but i don't know how to
implement it in this example. should someone help me?

Bye and thanks
 
M

Martin Honnen

Mariano said:
The only problem is, the using of XML document:

var utenti = xmldoc.documentElement;
var utente = utenti.firstChild;
var nodiUtente = utente.attributes;
var user = nodiUtente.getNamedItem("username");

this way is too "expansive". I would use XPATH but i don't know how to
implement it in this example. should someone help me?

The XPath expression is
/*/*[1]/@username
You can apply XPath with IE/MSXML using the selectNodes and
selectSingleNode method, with Mozilla and with Opera 9 there is the W3C
DOM Level 3 XPath API:
var displayText;
if (typeof xmldoc.selectSingleNode != 'undefined') {
displayText = xmldoc.selectSingleNode('/*/*[1]/@username').nodeValue;
}
else if (typeof xmdoc.evaluate != 'undefined') {
displayText = xmldoc.evaluate('/*/*[1]/@username', xmldoc, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.nodeValue;
}
As you can see there is a lot of code necessary to evaluate XPath for
the different implementations so you might want to write a function that
you can reuse.
 
D

David Mark

Then i have read an example on a book that explain how to read XML
node and return answer with an Ajax approach.
This is the js script:
_____________________________________
function checkUser() {
    var mozillaFlag = false;
    var XMLHttpRequestObject = false;
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
        XMLHttpRequestObject.overrideMimeType("text/xml");

Why are you doing this? Does your server not send the proper MIME
type? Also, this will fail in IE7 as its XHR implementation does not
support this method.
        mozillaFlag = true;

That is an invalid inference.
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new
        ActiveXObject("Microsoft.XMLHTTP");

This should be wrapped in a try-catch clause. ActiveX support can be
disabled in IE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top