Avatar of roy_sanu
roy_sanuFlag for India

asked on 

how to display xml data in html

Hi,

I have a  xml and i want to display the xml data in the html, it when i run in a browser it gives blank screen. what is the issue i donot
find. I have placed both the  employee.xml and the html file in a same folder

employees.xml
<?xml version ="1.0" encoding = "UTF-8"?>
<!-- this a comment -->
<employees>
<employee id ="1">
<first_name> john </first_name>
<last_name> Devon </last_name>
<date_of_joining>
<day>12</day>
<month>1</month>
<year>2014</year>
</date_of_joining>
<department role="the Manager">sales</department>
</employee>
</employees>

Open in new window


writing javascript  to fetch the xml data in the html

<html>
<head>
<script>
 var xmlObject, docObject, name, dateofJoining
  xmlObject = new XMLHttpRequest();
  xmlObject.open("GET","employees.xml",false);
  xmlObject.send();
  docObject = xmlObject.responseXML;
  name = docObject.getElementByTagName("first_name")[0].childNodes[0].nodeValue;
   document.write("Name = "+name+ </BR>");
   dateofJoining = docObject.getElementByTagName("day")[0].childNodes[0].nodeValue;
   dateofJoining = dateofJoining +"/"+docObject.getElementByTagName("month")[0].childNodes[0].nodeValue;
   dateofJoining = dateofJoining +"/"+docObject.getElementByTagName("year")[0].childNodes[0].nodeValue;
   document.write("DateofJoining= "+dateofJoining+"<BR/>");
  </script>
</head>
<body>
</body>
</html>

Open in new window

JavaScriptXMLHTML

Avatar of undefined
Last Comment
Dave Baldwin
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

There are too many problems with your code so I have created a demo from the code on this page:  http://www.w3schools.com/ajax/ajax_xmlhttprequest_response.asp

JS-XML2.html
<!DOCTYPE html>
<html>
<body>

<h2>My Employees:</h2>

<button type="button" onclick="loadDoc()">Get My Employees</button>

<p id="demo"></p>
 
<script>
function loadDoc() {
  var xhttp, xmlDoc, txt, x, i;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
  if (xhttp.readyState == 4 && xhttp.status == 200) {
    xmlDoc = xhttp.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("first_name");
    for (i = 0; i < x.length; i++) {
      txt = txt + x[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
    }
  };
  xhttp.open("GET", "employees.xml", true);
  xhttp.send();
}
</script>

</body>
</html>

Open in new window


employees.xml
<?xml version ="1.0" encoding = "UTF-8"?>
<!-- this a comment -->
<employees>
<employee id ="1">
<first_name>John</first_name>
<last_name>Devon</last_name>
<date_of_joining>
<day>12</day>
<month>1</month>
<year>2014</year>
</date_of_joining>
<department role="the Manager">sales</department>
</employee>
<employee id ="1">
<first_name>Roy</first_name>
<last_name>Sanu</last_name>
<date_of_joining>
<day>1</day>
<month>7</month>
<year>2015</year>
</date_of_joining>
<department role="the Manager">accounting</department>
</employee>
</employees>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo