2

I am using the below code to read one xml file which is located locally. But its not displaying the object of xmldoc. My code is

function loadXMLDoc(XMLname)
{
  var xmlDoc;
  if (window.XMLHttpRequest)
    {
    xmlDoc=new window.XMLHttpRequest();
    xmlDoc.open("GET",XMLname,false);
     xmlDoc.send("");
     return xmlDoc.responseXML;
   }

   else if (ActiveXObject("Microsoft.XMLDOM"))
   {
   xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
   xmlDoc.load(XMLname);
   return xmlDoc;
   }
   alert("Error loading document!");
   return null;
   }



   function f1()
   {
   var xmlDoc=loadXMLDoc(“test.xml”)
   var M = xmlDoc.getElementsByTagName(“article”);
   alert(M);
     }

Its not displaying the alert if i call the function f1.Thanks in advance

3
  • 2
    AJAX is asynchronous. Wait for xmlDoc to load, then parse its response data. Commented Aug 23, 2012 at 5:31
  • sorry i can't get u.I have to download anyother file for this?. whats mean by waiting for xmlDoc to load. Pls help Commented Aug 23, 2012 at 5:35
  • This is among the basics of AJAX. I would recommend you find an AJAX tutorial; it will explain better than I can. Commented Aug 23, 2012 at 5:41

1 Answer 1

10

Better use Jquery function. Its working fine for me.

<script src="jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$.ajax({
    type: "GET",
    url: "read2.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('site').each(function(){

            var id = $(this).attr('id');
            var title = $(this).find('title').text();
            var url = $(this).find('url').text();
            $(this).find('desc').each(function()
            {
                var brief = $(this).find('brief').text();
                var long = $(this).find('long').text();
                alert("my "+brief );
                alert("my "+long );

            });
        });
    }
});
});

And the XML file format will be

my title1 url1

brf 1 long 1

brf 2 long 2

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.