Home > other >  XPath for parsing xml - performance issue
XPath for parsing xml - performance issue

Time:11-29

I will have to call below function multiple times in order to display dynamic data (while displaying a graph). This function will be called like more than 50 times, for displaying a single graph page. Problem is it takes long time to parse xml and display graph page (like more than a minute).

If size of file needs to be taken into consideration. First xml file to be parsed is 140kb. Second xml is 8kb.

May I know where I am going wrong? and how I could improve performance in this scenario. Would appreciate any input.

function map(md, tls) {
        
        //First xml file parsing    
        var melXmlFile = "xml/Mel.xml"
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", melXmlFile , false);
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType('text/xml');
        }
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;
        .....

        //Second xml file parsing
        var msXmlFile = "xml/Ms.xml"
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", msXmlFile, false);
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType('text/xml');
        }
        xmlhttp.send();
        xmlMsDoc = xmlhttp.responseXML;
        ......
 }
     
   

Or should I be using different parsing style instead of XPath, so that performance is better? Thank you.

CodePudding user response:

In every function call you make two new XMLHttpRequests to load and parse the two XML input documents. As you said in a comment that the documents don't change in between calls to the function you can certainly improve things by doing the two requests only once and store the two documents and simply have your function perform the XPath evaluation on the stored documents.

  • Related