如何更快/更有效地使用ajax遍历xml文件?

时间:2021-08-31 14:28:04

i have an xml file that looks like the following:

我有一个xml文件,如下所示:

<UUT>
<DKBFile>091750</DKBFile>
<part>
  <name>FL_U1</name>
  <xcoord>439</xcoord>
  <ycoord>132</ycoord>
  <width>55</width>
  <height>24</height>
</part>
</UUT>

that may have a few hundred "Parts" that i'm trying to get name/coordinate info for. The amount, and the parts i'm getting information for, changes, but the traversing gets VERY slow once i'm trying to get this information for over 100 parts. Here's my code:

可能有几百个“部件”,我正在尝试获取名称/坐标信息。数量和我获取信息的部分会发生变化,但是一旦我试图获取超过100个部分的信息,遍历变得非常缓慢。这是我的代码:

$.ajax({
url: "PROFILER_ShopAid/shopaid.xml",
dataType: ($.browser.msie) ? "xml" : "text/xml",

success: function(data){    

var i = 1;  //array traversal
var xcoord = 0;
var ycoord = 0;
var width = 0;
var height = 0;
var part = "";
var nameFound = "";

//Get Part Count
var covCnt = DKB.GetClearedPartCnt();  //Gets number of parts

     //Get Part Info
while (covCnt >= i)
{
    part = DKB.GetClearedPart(i);  //Returns name of  part

    $(data).find("part").each(function() {
        nameFound = $(this).find("name").text();  

        if(part.toLowerCase() == nameFound.toLowerCase())
        {
            xcoord = parseInt( $(this).find("xcoord").text() );
            ycoord = parseInt( $(this).find("ycoord").text() );
            width = parseInt( $(this).find("width").text() );
            height = parseInt($(this).find("height").text() );

        }

    });
    i = i + 1;
}
},

error: function(){
}

}); 

The xml file is on a local server, this code is being used in an HTA (HTML Application file). Is there a more efficient way to go about traversing the xml file? right now, it takes about 15-20 seconds to get the information for ~150 parts. i've timed the queries i'm making to get the part count/part names (i commented out the AJAX code), and those are under 200ms for large part counts.

xml文件位于本地服务器上,此代码用于HTA(HTML应用程序文件)。是否有更有效的方法来遍历xml文件?现在,大约需要15-20秒来获取~150份的信息。我正在计算我正在进行的查询以获取零件数/零件名称(我注释掉了AJAX代码),并且这些查询大部分数量都在200毫秒以下。

any help would be greatly appreciated!

任何帮助将不胜感激!

1 个解决方案

#1


0  

You are parsing your entire XML data and creating a new object inside your loop when you do $(data).

当您执行$(数据)时,您正在解析整个XML数据并在循环内创建一个新对象。

Instead, you should put that before your loop and you should see a significant difference. Here is the relevant portion of your code updated:

相反,你应该把它放在你的循环之前,你应该看到一个显着的差异。以下是您更新的代码的相关部分:

...

var $data = $(data); // Do this once

while (covCnt >= i)
{

    part = DKB.GetClearedPart(i);  //Returns name of  part

    $data.find("part").each(function() {

    ...

#1


0  

You are parsing your entire XML data and creating a new object inside your loop when you do $(data).

当您执行$(数据)时,您正在解析整个XML数据并在循环内创建一个新对象。

Instead, you should put that before your loop and you should see a significant difference. Here is the relevant portion of your code updated:

相反,你应该把它放在你的循环之前,你应该看到一个显着的差异。以下是您更新的代码的相关部分:

...

var $data = $(data); // Do this once

while (covCnt >= i)
{

    part = DKB.GetClearedPart(i);  //Returns name of  part

    $data.find("part").each(function() {

    ...