SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    iterating xml, prototype,javascript

    ]I call via ajax and prototype.js a .xml file
    I want to fill with the xml files a combobox.
    How can I read all the elements of ths xml file with prototype or javascript?
    Code:
    function dofill( ) {   
      new Ajax.Updater( 'result', 'myyears.xml',
        { method: 'post', parameters: $('cmp').value,
      onSuccess: function( transport ) {
       // read the xml-fill the combobox
    
    
      } } );
    //So I fill a combobox
    //for( nOpt=0;nOpt<2;nOpt++) {
     
    //$('mdl').options[$('mdl').options.length] = new Option( '1' , 1 )
     //}
    }
    The xml file
    Code:
    <selectChoice>
    −
    	<entry>
    <optionText>test</optionText>
    <optionValue>1</optionValue>
    </entry>
    −
    	<entry>
    <optionText>Hello!</optionText>
    <optionValue>1</optionValue>
    </entry>
    </selectChoice>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give this a try:

    Code:
      onSuccess: function( transport ) {
      	var dom = transport.responseXML; // make sure your php sets application/xml as the content type
    		var entries = dom.getElementsByTagName("selectChoice")[0].getElementsByTagName("entry");
    		for (var i=0; i < entries.length; i++) {
    			var theText = entries[i].getElementsByTagName("optionText")[0].nodeValue;
    			var theValue = entries[i].getElementsByTagName("optionValue")[0].nodeValue;
    			alert("text: " + theText + ", value: " + theValue);
    		}
      }


Bookmarks

Posting Permissions

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