9
Answers

How to load XML file using in Javascript?

Hi,

how to load and read xml files using in javascript?
please my javascript function and replay me.

Below my code:-
function loadxml()
{
 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async = "false";
                    xmlDoc.load("Validation.xml");
                    var Validation = xmlDoc.documentElement;
                    alert(Validation);
                    var Validationtype = Validation.childNodes(0);
                    alert(Validationtype);
                    document.getElementById('Textbox1').innerHTML = Validation.getElementsByTagName("Name")[0].text;
}

Thanks,
Jitendra Patel
Answers (9)
3
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Jitendra,
Send below things
  1. Javascript you wrote.
  2. Error message you are getting.
  3. XML, if it's diffre from what u sent early

Because exact same code working without any roblem here :)
Accepted
0
Jitendra Patel

Jitendra Patel

NA 149 0 14y
Hi Dear,

Sorry to late replay.
you code is perfectly working.

It's my mistake.

Thanks a lots to solved my problem.

Thanks,
Jitendra Patel
0
Jitendra Patel

Jitendra Patel

NA 149 0 14y
hi,

I have used both but it is not working

xmlDoc.onreadystatechange = verify;

and

xmlDoc.onreadystatechange = verify();

after complete this line then print this

 alert(xmlDoc.onreadystatechange);

than errors.
Still it is not working.

0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Hi,
xmlDoc.onreadystatechange = verify();
 is wrong
xmlDoc.onreadystatechange = verify;
We are assigning the Ref. not value. I got xmlDocElement too with your XML. Let me know the updates.
0
Jitendra Patel

Jitendra Patel

NA 149 0 14y
Hi,

this function is not working.....
onreadystatechange()


than jQuery errors,
  xmlDoc.onreadystatechange = verify();
0
Jitendra Patel

Jitendra Patel

NA 149 0 14y
<?xml version="1.0" encoding="utf-8" ?>
<Validation>
  <Validationtype>
  <Text>Please Enter field.</Text>
  <Numeric>Please Enter Only Numeric value in field.</Numeric>
  </Validationtype>  
</Validation>
0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y
I am doubting your XML format. Send ur XML
0
Jitendra Patel

Jitendra Patel

NA 149 0 14y
hi,

when I use above code than
 in Loadxml function

 var xmlDocElement = xmlDoc.documentElement;

xmlDocElement is null.

it's value is not get........


Thanks,
Jitenra Patel
0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Hi,
Try this way. I am not sure where the road block is?
XML Used
<?xml version="1.0" ?> 
<employees>
<employee id="1" g="M">Jaish</employee>
<employee id="2" g="M">KD</employee>
<employee id="3" g="M">Aji</employee>
</employees>
Code
<script language="javascript" type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function pageLoad()
{
    loadxml("E:\\JAISH\\Validation.xml");
}
function loadxml(xmlFile)
{
    xmlDoc.async="false";
    xmlDoc.onreadystatechange=verify;
    xmlDoc.load(xmlFile);
    var xmlDocElement=xmlDoc.documentElement;
    alert(xmlDocElement.xml);
    var gender =  xmlDocElement.childNodes(0).attributes[1].value;
    alert(gender);
    var name =  xmlDocElement.childNodes(0).text
    alert(name)
}
//This is call back for any errors
function verify()
{
 // 0 Object is not initialized
 // 1 Loading object is loading data
 // 2 Loaded object has loaded data
 // 3 Data from object can be worked with
 // 4 Object completely initialized
 if (xmlDoc.readyState != 4)
 {
   return false;
 }
}

    </script>