Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Only getting blank screen

    I have an xml file with file name a.xml and a html file where I am doing the actual operation with filename a.html. Both xml file and html file is in D drive of my PC. This example loops through each <CD> element, and displays the values of the <ARTIST> and the <TITLE> elements in an HTML table.
    Contents of xml file are:

    Code:
    <?xml version="1.0"?>
    
    -<CATALOG>
    
    
    -<CD>
    
    <TITLE>Empire Burlesque</TITLE>
    
    <ARTIST>Bob Dylan</ARTIST>
    
    <COUNTRY>USA</COUNTRY>
    
    <COMPANY>Columbia</COMPANY>
    
    <PRICE>10.90</PRICE>
    
    <YEAR>1985</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Hide your heart</TITLE>
    
    <ARTIST>Bonnie Tyler</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>CBS Records</COMPANY>
    
    <PRICE>9.90</PRICE>
    
    <YEAR>1988</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Greatest Hits</TITLE>
    
    <ARTIST>Dolly Parton</ARTIST>
    
    <COUNTRY>USA</COUNTRY>
    
    <COMPANY>RCA</COMPANY>
    
    <PRICE>9.90</PRICE>
    
    <YEAR>1982</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Still got the blues</TITLE>
    
    <ARTIST>Gary Moore</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Virgin records</COMPANY>
    
    <PRICE>10.20</PRICE>
    
    <YEAR>1990</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Eros</TITLE>
    
    <ARTIST>Eros Ramazzotti</ARTIST>
    
    <COUNTRY>EU</COUNTRY>
    
    <COMPANY>BMG</COMPANY>
    
    <PRICE>9.90</PRICE>
    
    <YEAR>1997</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>One night only</TITLE>
    
    <ARTIST>Bee Gees</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Polydor</COMPANY>
    
    <PRICE>10.90</PRICE>
    
    <YEAR>1998</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Sylvias Mother</TITLE>
    
    <ARTIST>Dr.Hook</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>CBS</COMPANY>
    
    <PRICE>8.10</PRICE>
    
    <YEAR>1973</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Maggie May</TITLE>
    
    <ARTIST>Rod Stewart</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Pickwick</COMPANY>
    
    <PRICE>8.50</PRICE>
    
    <YEAR>1990</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Romanza</TITLE>
    
    <ARTIST>Andrea Bocelli</ARTIST>
    
    <COUNTRY>EU</COUNTRY>
    
    <COMPANY>Polydor</COMPANY>
    
    <PRICE>10.80</PRICE>
    
    <YEAR>1996</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>When a man loves a woman</TITLE>
    
    <ARTIST>Percy Sledge</ARTIST>
    
    <COUNTRY>USA</COUNTRY>
    
    <COMPANY>Atlantic</COMPANY>
    
    <PRICE>8.70</PRICE>
    
    <YEAR>1987</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Black angel</TITLE>
    
    <ARTIST>Savage Rose</ARTIST>
    
    <COUNTRY>EU</COUNTRY>
    
    <COMPANY>Mega</COMPANY>
    
    <PRICE>10.90</PRICE>
    
    <YEAR>1995</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>1999 Grammy Nominees</TITLE>
    
    <ARTIST>Many</ARTIST>
    
    <COUNTRY>USA</COUNTRY>
    
    <COMPANY>Grammy</COMPANY>
    
    <PRICE>10.20</PRICE>
    
    <YEAR>1999</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>For the good times</TITLE>
    
    <ARTIST>Kenny Rogers</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Mucik Master</COMPANY>
    
    <PRICE>8.70</PRICE>
    
    <YEAR>1995</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Big Willie style</TITLE>
    
    <ARTIST>Will Smith</ARTIST>
    
    <COUNTRY>USA</COUNTRY>
    
    <COMPANY>Columbia</COMPANY>
    
    <PRICE>9.90</PRICE>
    
    <YEAR>1997</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Tupelo Honey</TITLE>
    
    <ARTIST>Van Morrison</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Polydor</COMPANY>
    
    <PRICE>8.20</PRICE>
    
    <YEAR>1971</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Soulsville</TITLE>
    
    <ARTIST>Jorn Hoel</ARTIST>
    
    <COUNTRY>Norway</COUNTRY>
    
    <COMPANY>WEA</COMPANY>
    
    <PRICE>7.90</PRICE>
    
    <YEAR>1996</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>The very best of</TITLE>
    
    <ARTIST>Cat Stevens</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Island</COMPANY>
    
    <PRICE>8.90</PRICE>
    
    <YEAR>1990</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Stop</TITLE>
    
    <ARTIST>Sam Brown</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>A and M</COMPANY>
    
    <PRICE>8.90</PRICE>
    
    <YEAR>1988</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Bridge of Spies</TITLE>
    
    <ARTIST>T'Pau</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Siren</COMPANY>
    
    <PRICE>7.90</PRICE>
    
    <YEAR>1987</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Private Dancer</TITLE>
    
    <ARTIST>Tina Turner</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>Capitol</COMPANY>
    
    <PRICE>8.90</PRICE>
    
    <YEAR>1983</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Midt om natten</TITLE>
    
    <ARTIST>Kim Larsen</ARTIST>
    
    <COUNTRY>EU</COUNTRY>
    
    <COMPANY>Medley</COMPANY>
    
    <PRICE>7.80</PRICE>
    
    <YEAR>1983</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Pavarotti Gala Concert</TITLE>
    
    <ARTIST>Luciano Pavarotti</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>DECCA</COMPANY>
    
    <PRICE>9.90</PRICE>
    
    <YEAR>1991</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>The dock of the bay</TITLE>
    
    <ARTIST>Otis Redding</ARTIST>
    
    <COUNTRY>USA</COUNTRY>
    
    <COMPANY>Stax Records</COMPANY>
    
    <PRICE>7.90</PRICE>
    
    <YEAR>1968</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Picture book</TITLE>
    
    <ARTIST>Simply Red</ARTIST>
    
    <COUNTRY>EU</COUNTRY>
    
    <COMPANY>Elektra</COMPANY>
    
    <PRICE>7.20</PRICE>
    
    <YEAR>1985</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Red</TITLE>
    
    <ARTIST>The Communards</ARTIST>
    
    <COUNTRY>UK</COUNTRY>
    
    <COMPANY>London</COMPANY>
    
    <PRICE>7.80</PRICE>
    
    <YEAR>1987</YEAR>
    
    </CD>
    
    
    -<CD>
    
    <TITLE>Unchain my heart</TITLE>
    
    <ARTIST>Joe Cocker</ARTIST>
    
    <COUNTRY>USA</COUNTRY>
    
    <COMPANY>EMI</COMPANY>
    
    <PRICE>8.20</PRICE>
    
    <YEAR>1987</YEAR>
    
    </CD>
    
    </CATALOG>


    This is what I have done in HTML file:

    Code:
    <html>
    <head>
    <style>
    table, th, td {
      border: 1px solid black;
      border-collapse:collapse;
    }
    th, td {
      padding: 5px;
    }
    </style>
    </head>
    <body>
    
    <table id="demo"></table>
    
    <script>
    function loadXMLDoc() {
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          myFunction(this);
        }
      };
      xmlhttp.open("GET", "D:\\a.xml", true);
      xmlhttp.send();
    }
    function myFunction(xml) {
      var i;
      var xmlDoc = xml.responseXML;
      var table="<tr><th>Artist</th><th>Title</th></tr>";
      var x = xmlDoc.getElementsByTagName("CD");
      for (i = 0; i <x.length; i++) { 
        table += "<tr><td>" +
        x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
        "</td><td>" +
        x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
        "</td></tr>";
      }
      document.getElementById("demo").innerHTML = table;
    }
    </script>
    
    </body>
    </html>


    I am not getting any result. I can only see blank screen. Is it due to the file path?

  2. #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,526
    Thanks
    1
    Thanked 24 Times in 22 Posts
    Possibly. Try file://D:/a.xml
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, ES-Membrane project (Github Pages site)
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  3. #3
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    3,510
    Thanks
    3
    Thanked 626 Times in 612 Posts
    Hi there raushanaj5,

    and a warm welcome to these forums.

    check out the attachment which contains a fully working example.

    It will work locally in "Firefox" but other browsers, such as "Chrome",
    "IE11" and "Edge" require that the files be uploaded to your host's
    server or alternatively to a local server such as "XAMPP".

    coothead
    Attached Files Attached Files
    ~ the original bald headed old fart ~


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •