problem with XML & AJAX


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: problem with XML & AJAX

  1. #1
    Join Date
    Nov 2008
    Posts
    1

    problem with XML & AJAX

    Hi,

    I have an AJAX script that grabs some text from an xml file and posts it in a <div> on my webpage.

    The xml file looks like this:

    Code:
      <?xml version="1.0" ?> 
    - <root>
     - <message id="200830704.4951752">
         <text>Buy my junk!</text> 
       </message>
      </root>
    And this is the function that get's the text:

    Code:
    function handleReceiveText() {
     if (receiveReq.readyState == 4) {
      var text_div = document.getElementById('div_text');
      var xmldoc = receiveReq.responseXML;
      var message_nodes = xmldoc.getElementsByTagName("message"); 
      var n_messages = message_nodes.length
      for (i = 0; i < n_messages; i++) {
        var text_node = message_nodes[i].getElementsByTagName("text");
        text_div.innerHTML = '<div>' + text_node[0].firstChild.nodeValue + '</div>';
        lastMessage = (message_nodes[i].getAttribute('id'));
      }
      mTimer = setTimeout('getText();',120000); //Refresh the text in 2 minutes
     }
    }
    This works fine, but I'd like to have multiple items in multiple divs and I can't seem to figure out how to get it to work.

    I thought it might work if I added these lines:

    Code:
      <?xml version="1.0" ?> 
    - <root>
     - <message id="200830704.4951752">
         <text>Buy my junk!</text> 
       </message>
     - <image id="200830704.4951753">
         <img>junk.jpg</img> 
       </image>
      </root>
    And this is the function that get's the text:

    Code:
    function handleReceiveText() {
     if (receiveReq.readyState == 4) {
      var xmldoc = receiveReq.responseXML;
      var message_nodes = xmldoc.getElementsByTagName("message"); 
      var n_messages = message_nodes.length
      for (i = 0; i < n_messages; i++) {
        var text_node = message_nodes[i].getElementsByTagName("text");
        document.getElementById('div_text').innerHTML = '<div>' + text_node[0].firstChild.nodeValue + '</div>';
        lastMessage = (message_nodes[i].getAttribute('id'));
      }
      var img_div = document.getElementById('div_img');
      var n_images = image_nodes.length
      for (i = 0; i < n_images; i++) {
        var img_node = image_nodes[i].getElementsByTagName("img");
        image_nodes = xmldoc.getElementsByTagName("image").innerHTML = '<img src="' + img_node[0].firstChild.nodeValue + '">';
        lastMessage = (message_nodes[i].getAttribute('id'));
      }
      mTimer = setTimeout('getText();',120000); //Refresh the text in 2 minutes
     }
    }
    But it didn't work.

    I'd like it to be able to grab 4 different types of information from the xml file and then place each information into 4 different <div>s

    Can anyone tell me what I'm doing wrong?

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What happened when you ran it? Did you get any errors?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Problem applying simple XSLT to XML
    By gregclark in forum XML
    Replies: 3
    Last Post: 08-26-2005, 06:07 AM
  2. XML files vs Database
    By Mansoor in forum XML
    Replies: 8
    Last Post: 08-23-2002, 05:35 PM
  3. XML Startkabel Updates
    By xmlstartkabel in forum XML
    Replies: 0
    Last Post: 10-10-2001, 05:07 AM
  4. XML tutorials - fyi
    By Jeff Condon in forum XML
    Replies: 0
    Last Post: 05-29-2001, 03:25 PM
  5. Problem Converting ADO Recordset to XML
    By CHRISTOS STAVRINOU in forum Database
    Replies: 0
    Last Post: 11-16-2000, 05:56 PM

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center