How to get access XML file from Javascript (two ways and every thing bug )

— I try everything on internet and nothing work (Only vanilla CSS + XML + XSL) —

I am try to get access XML value from javaScript but it doesn’t work

First time I try to do it
https://www.w3schools.com/xml/xsl_client.asp

<script>
function loadXMLDoc(filename)
{
if (window.ActiveXObject)
  {
  xhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
else
  {
  xhttp = new XMLHttpRequest();
  }
xhttp.open("GET", filename, false);
try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}

function displayResult()
{
xml = loadXMLDoc("cdcatalog.xml");
xsl = loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
  {
  ex = xml.transformNode(xsl);
  document.getElementById("example").innerHTML = ex;
  }
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xsltProcessor = new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);
  resultDocument = xsltProcessor.transformToFragment(xml, document);
  document.getElementById("example").appendChild(resultDocument);
  }
}
</script>

And have error on that – xhttp.open("GET", filename, false); line.
Another way I am try do from this video https://www.youtube.com/watch?v=0gON4MUdJE8 with Loading XML Data Using Fetch, and he tell me SCRIPT1002: Syntax error on first line for example fetch('./Albums.xml').then(() => 2)

I share some code I hope you can help me fix that issue, only try to get value on javascript.

ok my XML

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="sheet.xsl"?>
<xmlTag>
blah blah blah
</xmlTag>

My XSL file

<?xml version="1.0"  encoding ="UTF-8" ?>
<xsl:stylesheet version="1.0 "
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/xmlTag">

        <head>
            <link rel="stylesheet" href="styles.css"/>
        </head>
        <html>
            <body id="id">
                 // some code
            </body>
        </html>

        <script src="./javaScript.js"></script> // link to my js file

    </xsl:template>
</xsl:stylesheet>

Js file

fetch('./Albums.xml').then(() => 2)

I need to change js file or approach (The connection between files)?
How can I get xml values from Js ?

Thanks guys really appreciate

69 thoughts on “How to get access XML file from Javascript (two ways and every thing bug )”

  1. Its such as you read my mind! You seem to grasp a lot approximately this, such as you wrote the
    book in it or something. I believe that you just can do
    with some p.c. to force the message house a bit,
    but instead of that, this is excellent blog. A great read.
    I’ll certainly be back.

    Reply
  2. Excellent post. I was checking constantly this weblog
    and I’m inspired! Extremely helpful information particularly the last section :
    ) I maintain such info a lot. I used to be seeking this certain information for a very lengthy time.
    Thanks and good luck.

    Reply