dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Getting an XML file using AJAX

  1. #1
    Join Date
    Dec 2007
    Posts
    1

    Question Getting an XML file using AJAX

    Hi,

    I have a requirement for reading an XML message, I wanted to retrieve an XML file that is sent as string using AJAX.

    A sample XML file that is sent would look like this
    <message><?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
    ...
    </message>

    I can only read the first line of this particular XML file

    var messageXML = req.responseXML.getElementsByTagName("message")[0];
    var messageText = messageXML.childNodes[0].nodeValue;

    messageText would be version="1.0" encoding="utf-8"

    But I wanted to read this XML file as a string. Any suggestions?

    Thanks
    Uma

  2. #2
    Join Date
    Nov 2007
    Posts
    11
    Try using req.responseText. I think that is the XML text.

  3. #3
    Join Date
    Jan 2008
    Posts
    1

    example with xml and ajax

    first the html file is as follows:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <script type="text/javascript" language="javascript">
    
                var http_request = false;
    
                function makeRequest(url) {
    
                    http_request = false;
    
                    if (window.XMLHttpRequest) { // Mozilla, Safari,...
                        http_request = new XMLHttpRequest();
                        if (http_request.overrideMimeType) {
                            http_request.overrideMimeType('text/xml');
                        }
                    } else if (window.ActiveXObject) { // IE
                        try {
                            http_request = new ActiveXObject("Msxml2.XMLHTTP");
                        } catch (e) {
                            try {
                            http_request = new ActiveXObject("Microsoft.XMLHTTP");
                            } catch (e) {}
                        }
                    }
    
                    if (!http_request) {
                        alert('Giving up :( Cannot create an XMLHTTP instance');
                        return false;
                    }
                    http_request.onreadystatechange = alertContents;
                    http_request.open('GET', url, true);
                    http_request.send(null);
    
                }
    
                function alertContents() {
    
                    if (http_request.readyState == 4) {
                        if (http_request.status == 200) {
                            var xmldoc = http_request.responseXML;
                            var root_node = xmldoc.getElementsByTagName('root').item(0);
                            alert(root_node.firstChild.data);
                        } else {
                            alert('There was a problem with the request.');
                        }
                    }
    
                }
            </script>
        </head>
        <body>
            <span
                style="cursor: pointer; text-decoration: underline"
                onclick="makeRequest('http://localhost/test.xml')">
                    Make a request
            </span>
        </body>
    </html>
    
    your xml file can be 
    <?xml version="1.0" ?>
    <root>
        I'm a test.
    </root>
    Hope this helps you

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by javagalaxy.com
    But I wanted to read this XML file as a string. Any suggestions?
    Does this help?

Similar Threads

  1. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 07:48 PM
  2. Help on Formating XML file ??
    By serge calderara in forum .NET
    Replies: 3
    Last Post: 04-12-2005, 01:10 PM
  3. Replies: 0
    Last Post: 09-02-2002, 06:11 AM
  4. How do i search within a xml file?
    By Kelvin Teh in forum XML
    Replies: 3
    Last Post: 02-16-2001, 11:09 AM
  5. Display xml file
    By rana in forum XML
    Replies: 5
    Last Post: 12-10-2000, 12:50 AM

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