Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中循环显示XML数据的更有效方法?_Javascript_Xml - Fatal编程技术网

在JavaScript中循环显示XML数据的更有效方法?

在JavaScript中循环显示XML数据的更有效方法?,javascript,xml,Javascript,Xml,请看一下我的JavaScript,让我知道是否有一种更有效的方法可以在我的XML中循环编写它 我的JavaScript代码如下所示: <script type="text/javascript"> function loadXMLDoc(dname) { xhttp=new XMLHttpRequest(); xhttp.open("GET",dname,false); xhttp.send(); return xhttp.responseXML; }

请看一下我的JavaScript,让我知道是否有一种更有效的方法可以在我的XML中循环编写它

我的JavaScript代码如下所示:

<script type="text/javascript">
function loadXMLDoc(dname)
{
    xhttp=new XMLHttpRequest();
    xhttp.open("GET",dname,false);
    xhttp.send();
    return xhttp.responseXML;
} 

var xmlDoc=loadXMLDoc("building.txt");

x=xmlDoc.getElementsByTagName('Building');

for (i=0;i<x.length;i++)
{
    txt=xmlDoc.getElementsByTagName('Building')[i].getAttribute('Name');

    document.write(txt + "<BR>");    

    y=x[i].getElementsByTagName('Tenant');

    for (j=0;j<y.length;j++)
    {
        txt1=x[i].getElementsByTagName('Tenant')[j].getAttribute('DISPLAYNAME');
        document.write("> " + txt1 + "<BR>");
    }

    document.write("<HR>");
} 

函数loadXMLDoc(dname)
{
xhttp=newXMLHttpRequest();
xhttp.open(“GET”、dname、false);
xhttp.send();
返回xhttp.responseXML;
} 
var xmlDoc=loadXMLDoc(“building.txt”);
x=xmlDoc.getElementsByTagName(“建筑”);
对于(i=0;i
  • 反向循环在这里不起作用,因为顺序不同(如果这对您很重要),但您可以通过缓存变量来提高循环性能:

    for (i=0; i<x.length; i++)  ->   for (var i=0,n=x.length; i<n; i++)
    for (j=0; j<y.length; j++)  ->   for (var j=0,m=y.length; j<m; j++)
    

    for(i=0;i for(var i=0,n=x.length;i更改为while

    var i = length; while(i--){...};
    

    我相信在javascript中,while比forloop快得多这里最慢的函数可能是
    xmlDoc.getElementsByTagName
    调用。对于正在查找的每个标记,只需调用xmlDoc.getElementsByTagName一次,而不是两次。然后可以在for循环中使用它并访问因此:第二次提到
    xmlDoc.getElementsByTagName('Building')
    时使用
    x
    ,第二次提到
    x[i]时使用
    y
    。getElementsByTagName('Tenant')
    。这将给你最大的加速

    另一个更改将简化您的代码,并允许您更轻松地进行优化(但本身可能不会有太多的加速):创建一个数据结构来保存数据,而不是在运行过程中为文档构建DOM元素。这还将使调试更容易。稍后,它将帮助您隔离性能问题——现在,即使使用探查器,也很难判断解析XML文档的速度慢还是构建新的DOM节点慢

    因此:

      var buildings = []; // add this
      for(...)
        txt=x[i].getAttribute('Name');
        // SKIP: document.write(txt + "<BR>");    
    
        y=x[i].getElementsByTagName('Tenant');
        var tenants = []; 
        for (j=0;j<y.length;j++)
        {
            txt1=y[j].getAttribute('DISPLAYNAME');
            tenants.push(txt1); // instead of: document.write("> " + txt1 + "<BR>");
        }
        buildings.push({ name: txt, tenants: tenants }); 
    
    var buildings=[];//添加此
    对于(…)
    txt=x[i].getAttribute('Name');
    //跳过:document.write(txt+“
    ”); y=x[i].getElementsByTagName(“租户”); var租户=[];
    对于(j=0;j优化版本,使用缓存:

    函数loadXMLDoc(dname) { xhttp=newXMLHttpRequest(); xhttp.open(“GET”、dname、false); xhttp.send(); 返回xhttp.responseXML; } var xmlDoc=loadXMLDoc(“building.txt”); var buildingTags=xmlDoc.getElementsByTagName('Building');
    对于(i=0,l=buildingTags.length;我看看我在自己的问题上发布的答案


    我成功创建了一个包含if-else语句的for循环,该循环将搜索xml并在表中显示结果。如果未找到结果,则会显示一条消息,表示未找到联系人。

    如我所述,这将返回与原始xml不同的顺序 function loadXMLDoc(dname) { xhttp=new XMLHttpRequest(); xhttp.open("GET",dname,false); xhttp.send(); return xhttp.responseXML; } var xmlDoc=loadXMLDoc("building.txt"); var buildingTags = xmlDoc.getElementsByTagName('Building'); for (i=0,l=buildingTags.length;i<l;i++){ txt=buildingTags[i].getAttribute('Name'); document.write(txt + "<BR>"); var y=buildingTags[i].getElementsByTagName('Tenant'); for (j=0,l1=y.length,j<l1;j++){ txt1=y[j].getAttribute('DISPLAYNAME'); document.write(" > " + txt1 + "<BR>"); } document.write("<HR>"); }