1 Replies - 440 Views - Last Post: 17 February 2015 - 11:30 AM

#1 methuselah90   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-December 14

Cannot load XML documents on web page with JavaScript

Posted 17 February 2015 - 11:21 AM

I am trying to load my parse my XML document but the HTML page remains null, is there something that I'm doing wrong.

index.html

<script type="text/javascript" src="assets/js/script.js"></script>
<body onload="outputXML('content', 'assets/xml/sample.xml', 'assets/xml/sample.xsl');"></body>


script.js

function loadXML (file) {
        if (window.XMLHttpRequest) {
                // code for Chrome, Firefox, Opera, etc.
                xhttp = new XMLHttpRequest();
        } else {
                // code for IE
                xhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Different ActiveXObject for IE
        };
        xhttp.open("GET", file, false);
        try {xhttp.responseType = "msxml-document";} catch (e) {}; // Set responseType for IE 9+
        xhttp.send(null);
        return xhttp.responseXML;
};
 
function processXML (location, xml, xsl) {
        if (window.ActiveXObject || xhttp.responseType == "msxml-document" || "ActiveXObject" in window) { // Added criteria for IE detection
                // code for IE
                ex = xml.transformNode(xsl);
                document.getElementById(location).innerHTML = ex;
        } else if (document.implementation && document.implementation.createDocument) {
                // code for Chrome, Firefox, Opera, etc.
                xsltProcessor = new XSLTProcessor();
                xsltProcessor.importStylesheet(xsl);
                resultDocument = xsltProcessor.transformToFragment(xml, document);
                document.getElementById(location).innerHTML = '';
                document.getElementById(location).appendChild(resultDocument);
        };
};
 
function outputXML(location, xmlFile, xslFile) {
        xml = loadXML(xmlFile);
        xsl = loadXML(xslFile);
        processXML(location, xml, xsl);
};
 
// Parse XML to String
function XMLToString(xml) {
        if (window.ActiveXObject) {
                // code for IE
                var txt = xml.xml;
                return txt;
        } else {
                // code for Chrome, Firefox, Opera, etc.
                var txt = new XMLSerializer();
                txt = txt.serializeToString(xml);
                return txt;
        };
};
 
function StringToXML(txt) {
        if (window.DOMParser) {
                // code for Chrome, Firefox, Opera, etc.
                parser=new DOMParser();
                xml=parser.parseFromString(txt,"text/xml");
        } else {
                // code for IE
                xml=new ActiveXObject("Microsoft.XMLDOM");
                xml.async=false;
                xml.loadXML(txt);
        };
        return xml;
};


sample.xml

<?xml version="1.0" encoding="utf-8"?>
<hitrecords>
  <record genre="Ska">
    <artist category="group" gender="male">Madness</artist>
    <title>Baggy Trousers</title>
    <length>
      <minutes>3</minutes>
      <seconds>30</seconds>
    </length>
  </record>
</hitrecords>


sample.xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="hitrecords/record">
            <p>
                <xsl:value-of select="."/>
                <xsl:value-of select="@genre"/> 
                <xsl:value-of select="artist"/> 
            </p>
        </xsl:for-each>
    </xsl:template> 
</xsl:stylesheet>


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot load XML documents on web page with JavaScript

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2465
  • View blog
  • Posts: 7,499
  • Joined: 15-January 14

Re: Cannot load XML documents on web page with JavaScript

Posted 17 February 2015 - 11:30 AM

If that's all of your HTML, then you're missing the element to output the contents. You should check on your error console, it would report errors like that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1