What I want to accomplish through javascript is to select, for instance, the Iowa state info and all of its data inside ref's node .
Javascript code:
Code:
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./xml/fotosus.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var objNodP = xmlDoc.getElementsByTagName('state');
var i=0;
var intParar=0;
while (intParar != 1) {
if (objNodP[i].getAttribute('id') == strState) {
var objNodR = xmlDoc.getElementsByTagName('ref');
for (n=0; n < objNodR.length; n++) {
number = objNodR.item(n);
attr = number.getAttribute('nr');
intParar = 1;
}
}
i++;
}
}
But I am having some difficulty to achieve only the data from the state ID.
The
Code:
objNodR.length
will show 3 instead of 2, because it retrieves from the XML file all the ref nodes instead only the ref nodes according to the state ID.
I'm not entirely sure what the intParar or i is doing, but it seems to me that you need more loops...
Code:
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./xml/fotosus.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var objNodP = xmlDoc.getElementsByTagName('state');
var i=0;
var intParar=0;
for (a=0; a < objNodP.length; a++) {
//while (intParar != 1) {
if (objNodP[a].getAttribute('id') == strState) {
alert(strState)
var objNodR = objNodP[a].getElementsByTagName('ref');
for (n=0; n < objNodR.length; n++) {
for (x=0; x < objNodR[n].children.length; x++) {
alert(objNodR[n].children[x].childNodes[0].data)
// intParar = 1;
//}
}
}
}
i++;
}
}
<?php
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./foto.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var objNodP = xmlDoc.getElementsByTagName('state');
var i=0;
var intParar=0;
for (a=0; a < objNodP.length; a++) {
//while (intParar != 1) {
if (objNodP[a].getAttribute('id') == strState) {
alert(strState)
var objNodR = objNodP[a].getElementsByTagName('ref');
for (n=0; n < objNodR.length; n++) {
for (x=0; x < objNodR[n].children.length; x++) {
alert(objNodR[n].children[x].childNodes[0].data)
// intParar = 1;
//}
}
}
}
i++;
}
}
?>
I tried the code and got an syntax error on line 3 ?
<script>
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./foto.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var objNodP = xmlDoc.getElementsByTagName('state');
var i=0;
var intParar=0;
for (a=0; a < objNodP.length; a++) {
//while (intParar != 1) {
if (objNodP[a].getAttribute('id') == strState) {
alert(strState)
var objNodR = objNodP[a].getElementsByTagName('ref');
for (n=0; n < objNodR.length; n++) {
for (x=0; x < objNodR[n].children.length; x++) {
alert(objNodR[n].children[x].childNodes[0].data)
// intParar = 1;
//}
}
}
}
i++;
}
}
funcFornecerDados("USAL");
</script>
I'm not entirely sure what the intParar or i is doing, but it seems to me that you need more loops...
Code:
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./xml/fotosus.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var objNodP = xmlDoc.getElementsByTagName('state');
var i=0;
var intParar=0;
for (a=0; a < objNodP.length; a++) {
//while (intParar != 1) {
if (objNodP[a].getAttribute('id') == strState) {
alert(strState)
var objNodR = objNodP[a].getElementsByTagName('ref');
for (n=0; n < objNodR.length; n++) {
for (x=0; x < objNodR[n].children.length; x++) {
alert(objNodR[n].children[x].childNodes[0].data)
// intParar = 1;
//}
}
}
}
i++;
}
}
Hello again XelaWho.
The code does not work in Chrome.
It seems it doesn't recognize the children property.
It returns Uncaught TypeError: Cannot read property 'length' of undefined:
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./xml/fotosus.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var objNodP = xmlDoc.getElementsByTagName('state');
var i=0;
var intParar=0;
for (a=0; a < objNodP.length; a++) {
if (objNodP[a].getAttribute('id') == strState) {
alert(strState)
var objNodR = objNodP[a].getElementsByTagName('ref');
for (n=0; n < objNodR.length; n++) {
for (x=0; x < objNodR[n].childElementCount; x++) {
alert(objNodR[n].childNodes[x].textContent;)
}
}
}
}
}
It works but some childnodes not representing an element get also caught.
Any workaround? My head seems like a hot potato protesting to blow!
to save you having to loop through again you can just do this:
Code:
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./xml/fotosus.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var states = xmlDoc.getElementsByTagName('state');
for (a=0; a < states.length; a++) {
if (states[a].getAttribute('id') == strState) {
var refs = states[a].getElementsByTagName('ref');
for (n=0; n < refs.length; n++) {
for (x=0; x < refs[n].childNodes.length; x++) {
if(refs[n].childNodes[x].nodeType==1){
alert(refs[n].childNodes[x].firstChild.textContent)
}
}
}
}
}
}
to save you having to loop through again you can just do this:
Code:
function funcFornecerDados (strState) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET','./xml/fotosus.xml',false);
httpRequest.send(null);
var xmlDoc = httpRequest.responseXML;
var states = xmlDoc.getElementsByTagName('state');
for (a=0; a < states.length; a++) {
if (states[a].getAttribute('id') == strState) {
var refs = states[a].getElementsByTagName('ref');
for (n=0; n < refs.length; n++) {
for (x=0; x < refs[n].childNodes.length; x++) {
if(refs[n].childNodes[x].nodeType==1){
alert(refs[n].childNodes[x].firstChild.textContent)
}
}
}
}
}
}
MUCHAS GRACIAS XELAWHO!!
It seems to work in FF. Gotta try the others... not betting on IE though, because that's another story... (other errors).
I had reached the same code about objNodR[intN].childNodes[intX].nodeType == 1 but the firstChild method was the MISSING part!
Last edited by fowler1979; 05-03-2012 at 11:54 AM.
Bookmarks