www.webdeveloper.com
Results 1 to 14 of 14

Thread: Interacting through a XML file

  1. #1
    Join Date
    Apr 2012
    Posts
    17

    Post Interacting through a XML file

    Hello everybody.
    My first time here and I'll hope we all can help each other accordingly.

    My problem may be quite simple. I need to interact over a XML file and retrieve some data.
    The XML layout is the following:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <info>
    	<state id="USAL"> <!-- Alabama, US -->
    		<ref nr="000001">
    			<titl>Wilks Factory, Cloverdale, Tuscaloosa county</titl>
    			<mail>test@aol.us</mail>
    			<aut>Keith Turnpike</aut>
    			<data>February 2010</data>
    		</ref>
    	</state>
    	<state id="USIA"> <!-- Iowa, US -->
    		<ref nr="000001">
    			<titl>Don Distellery, Waucoma, Fayette county</titl>
    			<mail>teste@aol.pt</mail>
    			<aut>Jim Sheridan</aut>
    			<data>August 7, 2011</data>
    		</ref>
    		<ref nr="000002">
    			<titl>Tammill Bakery, Searsboro, Poweshiek county</titl>
    			<mail>test2@aol.us</mail>
    			<aut>Aldus Sheikyrn</aut>
    			<data>January 20, 2012</data>
    		</ref>
    	</state>
    </info>
    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.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    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++;
    	}
    }

  3. #3
    Join Date
    Apr 2012
    Posts
    17
    So simple, yet it turn my brain into a mush...
    Thank you so much XelaWho, it worked like a charm.

    The intParar and the i was to loop interact with the state node... quite inneficient I think. Anyway, thanks again.

  4. #4
    Join Date
    Nov 2011
    Posts
    64
    Code:
    <?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 ?

  5. #5
    Join Date
    Nov 2010
    Posts
    1,084
    ... works for us...

    what's on line 3 of your code?

  6. #6
    Join Date
    Nov 2011
    Posts
    64
    Code:
    function funcFornecerDados (strState) {
    line 3
    Parse error: syntax error, unexpected ')', expecting '&' or T_VARIABLE in /home2/public_html/testing.php on line 3
    Last edited by stevengoh; 04-27-2012 at 12:51 PM.

  7. #7
    Join Date
    Nov 2010
    Posts
    1,084
    looks like a php error to me

  8. #8
    Join Date
    Nov 2011
    Posts
    64
    Code:
    <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 didn't see execution of the script ?

  9. #9
    Join Date
    Nov 2010
    Posts
    1,084
    ah. well, being that this is the javascript forum, certain assumptions can be made...

  10. #10
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by xelawho View Post
    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:
    Code:
    for (x=0; x < objNodR[n].children.length; x++) {

  11. #11
    Join Date
    Apr 2012
    Posts
    17
    I have changed this:
    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++) {
    
    		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!

  12. #12
    Join Date
    Nov 2010
    Posts
    1,084
    calmate, hombre

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

  13. #13
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by xelawho View Post
    calmate, hombre

    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.

  14. #14
    Join Date
    Nov 2010
    Posts
    1,084
    Quote Originally Posted by fowler1979 View Post
    not betting on IE though, because that's another story... (other errors)
    oh, yeah - I forgot about pinche IE. this seems to work in Chrome, FF and IE:
    Code:
    alert(refs[n].childNodes[x].firstChild.nodeValue)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles