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
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