2

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.

7
  • You could do an XMLHttpRequest developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/… Commented Apr 4, 2018 at 22:07
  • For parsing, take a look here: w3schools.com/xml/xml_parser.asp Commented Apr 4, 2018 at 22:08
  • do you mind to elaborate a little more please? Commented Apr 4, 2018 at 22:15
  • Working on an answer right now. Hopefully it will help, if not, I have failed the Stack Overflow gods. Commented Apr 4, 2018 at 22:15
  • I'm deleting my answer I obviously can't code correctly when I have no way of debugging the darn thing. gl my friend Commented Apr 4, 2018 at 22:39

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

But thats not for getting the data from an external file.
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.