Extracting XML and Comparing it to an external document

Hi -
I have a function that extracts XML data and stores it in a variable using local storage. (Thanks to @MarkHunte for that!).

Now I need to compare that extracted XML data to an external XML file that I have in my resource folder. Then, do something simple if the data does or does not match.
Is this possible and how would I do it?

I’ve attached an example file. In this example, I’m trying to compare the extracted data to NFExample.xml.

Thank you! Noteflight XML DemoV4.hype.zip (81.7 KB)

Without having a deep look. at the mo.

You should be able to read you local file probably as text and use DOMParser() to get it as an object.

Also you can do the same with the external score data.

    if (typeof window.theScoreData == "undefined") {window.theScoreData= {} };

    window.scoreView.getMusicXML().done(function(scoreData) {
   
   window.theScoreData = scoreData
    // do something with the returned score data
    
 
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(scoreData, "application/xml");
        
           console.log(xmlDoc)
      
      });

Then comapre them.

quick example ( basically hacking some of my bits and pieces together ) and most likely not the only way…

Called on a button action for example

      const localXML = '${resourcesFolderName}/maineTestColor.xml'
	   
	   window.scoreView.getMusicXML().done(function(scoreData) {
   
   
     hypeDocument.scoreData  = scoreData
    
  });

	 
 	    async   function getXMLS() {
 	    //-- Only  run this part once
		 if(typeof   hypeDocument.xml_ == "undefined") { 
		 
		 const response = await fetch(localXML);
            
        const data = await response.text();
            
            
            
   parser = new DOMParser();
        xmlDoc = parser.parseFromString(data, "application/xml");
         
     hypeDocument.xml_ = xmlDoc 
     
      hypeDocument.theScoreDataXML    =  parser.parseFromString(   hypeDocument.scoreData , "application/xml"); 
    } 
    
  
 compare()
} 


//-- start
getXMLS()
 
 function compare(){
 var instrument_sound_1 = hypeDocument.xml_.getElementsByTagName("instrument-sound")[0].childNodes[0].nodeValue;
 var instrument_sound_2 = hypeDocument.theScoreDataXML.getElementsByTagName("instrument-sound")[0].childNodes[0].nodeValue;
var resu_ = hypeDocument.getElementById('resu')

 if (instrument_sound_1 == instrument_sound_2 ){
resu_.style.backgroundColor = "green"
 
 } else {
 
 resu_.style.backgroundColor = "red"
 }

 
}

Thank you so much @MarkHunte !

I tried to set it up with a button function as you’v suggested but for some reason I’m getting an error: Uncaught (in promise) TypeError: Cannot read property ‘childNodes’ of undefined

Noteflight XML DemoV6.hype.zip (83.1 KB)
Is there something else that I am doing incorrectly?
Thank you!
Matt

lol,

When I just looked at your doc… I thought huh, that odd, he put a resu element/id to check the result the same as I did in my ( not posted )project file.

Then remembered I did that left that in the example code… doh.

Any way you did everything right and it works this end.

The problem will be that your noteflight is not fully loaded when you check.

You need to let it load fully first before you check. remember t you are asking the window.scoreView.getMusicXML() in there and that has to exist first.

The code is just an idea/example but you will need to work on when/how you safely do a check.
I would expect note flight to have some load complete api.

Hi Mark -
Thanks so much. I’m waiting forever and the score is loaded but I’m still not getting a result.

The Noteflight API for when the score is fully loaded is: scoreDataLoaded
I’m not exactly where to put that in your code. (I’m close but I don’t exactly understand what is happening yet!) :slight_smile:
Thank you so much!
Matt

That is odd, I have no issue my end and only wait a second or so. My testing is local with cross domain disabled on the page. Otherwise nothing loads.

As yo see here.

Ignore all the red error those are because I am running it locally and do not have access to what ever it needs there.

but below you can see I get results in the console as well as the rect turning green.

Thanks for this @MarkHunte .
So very strange!
I’m working at a location with VERY slow internet access. Could this be causing me a problem somehow?
(The score data including instrument-sound is loading completely though before I run the compare.)

Thanks!
Matt

UPDATE:
It works in Safari but not the latest version of Chrome.
Can anyone say why this might be and what I need to do for it to work in Chrome?

Thank you!
Matt

What do you see in Chrome's developer console? ( ⌘ + shift + i ) ?