工作,但非常慢…用JS调用XML,然后向XML中的文本添加JS函数
Works, but is VERY slow... calling an XML with JS and then adding a JS function to the text from the XML
我用JS从XML文件中调用文本,然后向该文本添加JS函数。然而,反应非常非常慢。我能不能改进一下?
我有两个XML文件…一个是我创建的,另一个是用flash程序创建的数据库。我想突出这个flash程序的某些功能。我有三种方法:
-
我手动输入文本并使用JS突出显示flash功能onmouseover(速度很棒,但我必须手动输入html页面上的所有文本,并且必须为每个单个flash文件这样做(我正在做50多个)此外,数据会频繁更改,最好有一个数据库文档,而不是单独进入每个flash文件。
-
我在包含JS的XML文件中创建了一个标记,当我从页面调用它时,整个列表立即填充,但它非常慢,每次鼠标移到每个文本上需要几秒钟
-
我用JS调用XML文本,并为鼠标悬停添加另一个JS函数。这工作得很好,但很慢(然而不是像#2那样慢,但仍然很慢),JS很长,因为我必须单独调用每个文本,而不是像我在#2
中那样填充整个列表。
下面是2和3的例子:
XML for #2
<VIRGINIAREP>
<DISTRICTS>
<STATE>Virginia</STATE>
<DISTRICT>1</DISTRICT>
<REPRESENTATIVE>Robert J. Wittman</REPRESENTATIVE>
<REPWEB>http://wittman.house.gov' onMouseOver='highlight1()' onMouseOut='highlight_clear()'</REPWEB>
<PARTY>R</PARTY>
</DISTRICTS>
<DISTRICTS>
<STATE>Virginia</STATE>
<DISTRICT>2</DISTRICT>
<REPRESENTATIVE>Scott Rigell</REPRESENTATIVE>
<REPWEB>http://rigell.house.gov' onMouseOver='highlight2()' onMouseOut='highlight_clear()'</REPWEB>
<PARTY>R</PARTY>
</DISTRICTS>
<DISTRICTS>
<STATE>Virginia</STATE>
<DISTRICT>3</DISTRICT>
<REPRESENTATIVE>Robert C. Scott</REPRESENTATIVE>
<REPWEB>http://www.house.gov/scott/' onMouseOver='highlight3()' onMouseOut='highlight_clear()'</REPWEB>
<PARTY>D</PARTY>
</DISTRICTS>..... etc.
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","113CongressC.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='0'>");
var x=xmlDoc.getElementsByTagName("DISTRICTS");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("DISTRICT")[0].childNodes[0].nodeValue + " -");
document.write("</td><td>");
document.write("<a TARGET='_blank' href='" + x[i].getElementsByTagName("REPWEB")[0].childNodes[0].nodeValue + "'>" + x[i].getElementsByTagName("REPRESENTATIVE")[0].childNodes[0].nodeValue) + "</a>";
document.write("</td><td>");
document.write("(" + x[i].getElementsByTagName("PARTY")[0].childNodes[0].nodeValue + ")");
document.write("</td></tr>");
}
document.write("</table>");
</script>
HIGHLIGHT FEATURE
function highlight() {
theMap.features('DISTRICT="1"').highlight(
{visible: true, fillColor: "#FFFF00", fillAlpha: 1.0}
);
}
XML for #3
<VIRGINIAREP>
<DISTRICTS>
<STATE>Virginia</STATE>
<DISTRICT>1</DISTRICT>
<REPRESENTATIVE>Robert Wittman</REPRESENTATIVE>
<REPWEB>http://wittman.house.gov</REPWEB>
<PARTY>R</PARTY>
</DISTRICTS> ...etc.
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","113CongressC.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
x=xmlDoc.getElementsByTagName("DISTRICTS");
i=0;
function displayREPS()
{
rep="<a target = '_blank' onMouseOver='highlight()' onMouseOut='highlight_clear()' href = '" + (x[0].getElementsByTagName("REPWEB")[0].childNodes[0].nodeValue) + "'>" +(x[0].getElementsByTagName("REPRESENTATIVE")[0].childNodes[0].nodeValue) + "</a>";
district=(x[0].getElementsByTagName("DISTRICT")[0].childNodes[0].nodeValue);
party=(x[0].getElementsByTagName("PARTY")[0].childNodes[0].nodeValue);
//2
rep1="<a target = '_blank' onMouseOver='highlight1()' onMouseOut='highlight_clear()' href = '" + (x[1].getElementsByTagName("REPWEB")[0].childNodes[0].nodeValue) + "'>" +(x[1].getElementsByTagName("REPRESENTATIVE")[0].childNodes[0].nodeValue) + "</a>";
district1=(x[1].getElementsByTagName("DISTRICT")[0].childNodes[0].nodeValue);
party1=(x[1].getElementsByTagName("PARTY")[0].childNodes[0].nodeValue);
//3
rep2="<a target = '_blank' onMouseOver='highlight2()' onMouseOut='highlight_clear()' href = '" + (x[2].getElementsByTagName("REPWEB")[0].childNodes[0].nodeValue) + "'>" +(x[2].getElementsByTagName("REPRESENTATIVE")[0].childNodes[0].nodeValue) + "</a>";
district2=(x[2].getElementsByTagName("DISTRICT")[0].childNodes[0].nodeValue);
party2=(x[2].getElementsByTagName("PARTY")[0].childNodes[0].nodeValue);
…等。
//TXT
txt0=district + " - " + rep + " ("+ party + ")";
txt1=district1 + " - " + rep1 + " ("+ party1 + ")";
txt2=district2 + " - " + rep2 + " ("+ party2 + ")";
txt3=district3 + " - " + rep3 + " ("+ party3 + ")";
txt4=district4 + " - " + rep4 + " ("+ party4 + ")";
...etc
document.getElementById("showREPS").innerHTML=txt0 + "<br>" + txt1 + "<br>" + txt2 + "<br>" + txt3 + "<br>" + txt4 + "<br>" + txt5 + "<br>" + txt6 + "<br>" + txt7 + "<br>" + txt8 + "<br>" + txt9 + "<br>" + txt10 + "<br>";
}
</script>
</head>
<body onload="displayREPS()">
<div id='showREPS'></div>
我也尝试过:<REPWEB><![CDATA[<a target='_blank' href='http://wittman.house.gov' onMouseOver='highlight0()' onMouseOut='highlight_clear()'>]]></REPWEB>
用于XML
有什么建议,我如何才能做得更好,更有效地与快速?
我在COM代码中的XHR对象中发现了同样的问题。当我使用responseXML方法而不是responseText时,xml文档使用XPath查询查找节点的速度非常慢。我使用的是MSXML6。
- 使用responseText获取整个文本
- 创建DOM文档
- 通过新DOM文档的loadXML(string)方法加载xml
瞧!真快!我不知道为什么XMLHTTPRequest会在responseXML上创建什么样的DOM文档才能得到这么慢的DOM文档但创建新文档会更快
我会考虑使用JSON格式,而不是XML格式。它的传输速度要快得多(就字节而言它更小),JSON的解析速度也要快得多。
- 如何在Node.js中根据XML验证DTD
- xml文件在js项目目录结构中的位置
- Mongo-JS-如何将BSON/JSON对象转换为XML
- 通过JS/AAJAX在XML文件中搜索1个数据
- 在Drupal7中扩展字段集时,修改collapse.js以从xml获取附加数据
- Windows 8市场应用程序JS,访问远程XML文件
- 如何在 JavaScript 节点中将 xml 文件解析为数组.js
- 如何在node.js中返回与URL请求相对应的XML文件
- 带有ajax xml请求的Node js脚本不起作用
- 使用JS'更正语法;onclick'PHP echo语句中的函数与嵌入的XML获取进行AND
- 从js文件而不是xml文件创建关联数组
- 解析 XML 到 js 数据,如何访问特定标记
- 如何在 Node.js 中使用 rdflib.js 或 rdf-parser-rdfxml 解析 RDF/XML
- 在 JS 中创建、编辑和保存 XML
- XML 数据转换为 PHP 或 JS
- 使用 JS 将 XML 数据输出到 HTML
- Angular JS XML数据显示问题
- node.js xml to json objects
- JS XML解析:值不变
- 修复JS XML加载不使用async=false - how