简体   繁体   中英

Load data from an external xml file with javascript or jquery

Hi,

I have an xml file that looks like this:

<?xml version="1.0"?>
<sendSound enable="true" autoPlay="true">
    <item name="Gasp for surprise" src="flashsound/gasp.mp3"></item>
    <item name="Giggle" src="flashsound/hehe.mp3"></item>
    <item name="Say hello" src="flashsound/hello.mp3"></item>
</sendSound>

I want to extract the data to get a list like this on the console logs

Gasp for surprise
Giggle
Say hello

how can I do it with javascript or jquery? This is my code so far:

var users = xml.getElementsByTagName("sendSound");
for(var i = 0; i < users.length; i++) {
    var user = users[i];
    var names = user.getElementsByTagName("item");
    for(var j = 0; j < names.length; j++) {
        console.log(names[j].getAttribute("name"));
    }
}

Thank you.

This is with jQuery and a little vanilla.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery.parseXML demo</title>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>


<script>
    $.get( "./file.xml", function( data ) {
    var xml = new XMLSerializer().serializeToString(data);
    var xmlDoc = $.parseXML( xml );
    var xml = $( xmlDoc );
    var users = xml.find( "sendSound" );

    for(var i = 0; i < users.length; i++) {
        var user = users[i];
        var names = user.getElementsByTagName("item");
        for(var j = 0; j < names.length; j++) {
            console.log(names[j].getAttribute("name"));
        }
    }
});

</body>
</html>

I found this solution for achieving this without dependencies like jQuery, but it needed a bit of tweaking so here's my version:

function XMLtransformation(xslUrl, xmlUrl) {
    const errorMessage = 'Unable to load the content';
    const parser = new DOMParser();

    // attempt to load the XSL file
    const xslRequest = new XMLHttpRequest();
    xslRequest.open('GET', xslUrl, false);  // `false` makes the request synchronous
    xslRequest.send(null);

    if (xslRequest.status < 300) {
        const xslStylesheet = parser.parseFromString(xslRequest.response, "application/xml");
        const xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(xslStylesheet);

        const xmlRequest = new XMLHttpRequest();
        xmlRequest.open('GET', xmlUrl, false);
        xmlRequest.send(null);

        if (xmlRequest.status < 300) {
            const htmlDocument = xsltProcessor.transformToDocument(
                parser.parseFromString(xmlRequest.response, "application/xml"),
                document
            );

            return htmlDocument.documentElement.outerHTML;
        } else {
            console.error('xml load failure:');
            console.error(xmlRequest.status, xmlRequest.responseText);
        }
    } else {
        console.error('xsl load failure:');
        console.error(xslRequest.status, xslRequest.responseText);
    }

    return errorMessage;
}

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