简体   繁体   中英

Get the XML data into HTML page using http get request

I need to fetch the data from the XML file. That XML file would contain simply details of a user, ie datails can be anything.. user name and email id.. or date of birth.. etc..

Here I create a HTML source code having 2 text boxes.. Details are :- First text box I enter the name: ABC

In the second text box, the email id of that user must appear by fetching the XML content automatically.

I have read about the http get request and post request but unable to make them.

Kindly help me..

Here is the HTML code : -

    <html>
    <head>
    <title> Sample XML page </title>
    </head>
    <body>
  <div align="right">
    <h1>
     Sample try page 
   </h1>

   <form name="login">
    Username: &nbsp 
    <input type="text" name="userid"/>
    <br>
    <br>
    Email:  &nbsp  
    <input type="text" name="Email"/>
    <br>
    <br>
    <input type="button" " value="Submit"/>
   </form>
   </div>
   </body>
   </html>

Here is the XML code : -

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<student>  

<details>    
 <Name> student1 </Name>    
 <email> student1@abc.com </email>
 </details>

 <details>    
 <Name> student2 </Name> 
 <email> student2@abc.com </email>
 </details>

 </student>

From W3 schools

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","books.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

That is how you turn the XML into a Javscript object from a URL.

Hope this can help you bit as part of JavaScript -

<script type="text/javascript">
    var useName;
    var email;

    function httprequest() 
    {
        debugger;
        useName = login.txtUserId.value;
        xmlhttp = new XMLHttpRequest();

        xmlhttp.open("GET", "name.xml", false);
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;

        var x = xmlDoc.getElementsByTagName("CD"); 
        debugger;
        for (i = 0; i < x.length; i++) 
        {
            if (useName == x[i].getElementsByTagName("detail1")[0].childNodes[0].nodeValue) 
            {
                login.txtEmail.value = x[i].getElementsByTagName("detail2")[0].childNodes[0].data;
            }
        }
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM