1

Let's suppose I have an XML file like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<MIDIFile>

  <Event>
    <Absolute>0</Absolute>
    <NoteOn Channel="1" Note="40" Velocity="64"/>
  </Event>
  <Event>
    <Absolute>236</Absolute>
    <NoteOff Channel="1" Note="40" Velocity="0"/>
  </Event>

</MIDIFile>

Thanks to some great tutorial I now know how to get these values in my javascript.

For example, I can iterate thru all the "Events" tag and get the "Absolute" value like this:

xmlhttp=new XMLHttpRequest();

xmlhttp.open("GET","test.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

// make an array with all the events from the xml file
var events = xmlDoc.getElementsByTagName("Event")


for (var i = 0; i<events.length; i++)
console.log(events[i].getElementsByTagName("Absolute")[0].childNodes[0].nodeValue)

this will return

0
236

Now, how the hell can I access the "NoteOn" attribute and get its values? I want to iterate thru all the Events in the XML, see if they contains NoteOn or NoteOff and load an array with all the notes, their duration, velocity and channels.

Please help me! getElementsByTagName("NoteOn") just doesn't work... If it might help, here are some screenshot of what happens if I console.log(xmlDoc)

Thanks a lot in advance!

screen


edit in response to an answer. As I try to do this:

var noteon = xmlDoc.getElementsByTagName('noteon');
console.log(noteon)

the result is just this one

[]

re-edit: If I write "NoteOn" instead of "noteon" it works!

2
  • Can you use jQuery? and what value of NoteOn do you want to extract? Commented May 23, 2012 at 23:19
  • 2
    He doesn't need jQuery it takes 4 lines of code or less to get the attributes. just as I have posted below. Commented May 23, 2012 at 23:26

1 Answer 1

4
var noteon = xmlDoc.getElementsByTagName('noteon');
var note = new Array;
for(var i = 0; i < noteon.length; i++){
    note[i] = noteon[i].getAttribute('Note'); // or any of the attributes you want to get
}

Edit:

If noteon = [] then you have 2 options you can put the whole thing in a try catch, not the best thing to do in javascript, or you can put it in an if statement. Something like this should work:

var noteon = xmlDoc.getElementsByTagName('noteon');
var noteoff = xmlDoc.getElementsByTagName('noteoff');
var note = new Array;

if(noteon != []){
    for(var i = 0; i < noteon.length; i++){
        note[i] = noteon[i].getAttribute('Note'); // or any of the attributes you want to get
} else if(noteoff != []){
    for(var i = 0; i < noteoff.length; i++){
        note[i] = noteoff[i].getAttribute('Note'); // or any of the attributes you want to get
} else{
    return;  //makes sure the function returns to the call even if nothing was found
}
}
Sign up to request clarification or add additional context in comments.

7 Comments

He may wants all the Noteon or Noteoff within the events and not from the XML document.
it doesn't work, see edited answer. Thanks a lot anyway for your help!
Cool! It works if I write "NoteOn" instead of "noteon". Thanks a lot!
what if I try to access NoteOn inside of an event and it doesn't exist (because there is NoteOff and not NoteOn)? It trows error and stop the entire script :( I think I'll write a new question...
@mistapink what do you mean? Noteon and noteoff are nodes of the xml document.
|

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.