您的位置:首页 > Web前端 > JQuery

jQuery 1.5 xml 遍历所有节点属性

2011-02-16 10:32 393 查看
项目中原先的方法:

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";

xmlDoc.loadXML(LegendData);
if (xmlDoc.xml != "") {
//添加根节点
xmlDocRoot = xmlDoc.documentElement;

var svgMapGNode = svgMapDoc.createElementNS(svgNS,"g");
var sAttName, sAttValue;
var sNodeName, sNodeData;
var xmlNode;

//得到
for (var i = 0; i < xmlDocRoot.attributes.length; i++) {
sAttName = xmlDocRoot.attributes(i).name;
sAttValue = xmlDocRoot.attributes(i).value;
svgMapGNode.setAttributeNS(null,sAttName, sAttValue);
}

iCount = xmlDocRoot.childNodes.length;

//循环从XML文件中添加图形对象
for (i = 0; i < iCount; i++) {
xmlNode = xmlDocRoot.childNodes(i);
sNodeName = xmlNode.nodeName;
sNodeData = xmlNode.text;

}

考虑到浏览器的兼容性,使用jQuery 1.5来实现上面的功能

<html xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Student Survey</title>
<script type="text/javascript" src="jquery-1.5.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var sxx;

//LegendData 的内容就是下面xml的内容
var xml = '<g id="road" onmousemove="OnMapMouseMove(evt)" onmouseout="OnMapMouseOut(evt)" style="visibility:visible"><path class="roadStyle roadNational" id="rp1" d="m 2846.4737,1600.5819 c -2.9681,4.9507 -0.8251,5.7206 -7.2486,14.5732 -4.404,6.0696 -16.6266,14.1496 -23.9819,22.3159 -4.6725,5.1879 -5.9048,10.6214 -6.6632,14.9189 -5.178,29.3425 -2.0169,44.3559 -6.3489,52.1266 -16.4484,29.5045 -37.9516,70.4041 -41.1451,72.9337 -18.1493,14.3762 -47.5008,33.591 -43.7873,49.6901 1.4402,6.2438 2.7623,10.8105 3.8339,12.5028 10.6402,16.8031 26.0335,40.1837 18.9098,48.2035 -11.7603,13.2395 -15.4703,12.1432 -25.423,26.1862 -5.5487,7.8291 -10.5114,14.1048 -15.0833,21.0574 -2.5518,3.8807 -2.0823,6.8343 -4.1683,10.1733 -4.0663,6.509 -6.4184,9.4246 -7.8909,12.9858 -5.1876,12.5463 -10.1436,24.3344 -9.5452,37.0842 0.6883,14.6676 -1.588,27.0011 -4.922,31.5637 -9.775,13.3773 -17.8985,25.6711 -23.7864,36.3241 -7.2319,13.0849 -9.7824,27.5903 -15.4516,33.3626 -15.134,15.4094 -30.5944,31.5097 -30.7506,36.6202 -0.2836,9.2764 -1.3587,19.013 -4.8594,23.3239 -4.6809,5.7641 -8.8777,11.7616 -10.2996,16.6926 -2.52,8.7393 -5.1649,16.8491 -7.5614,19.6874 -3.8817,4.5972 -13.8997,7.1987 -17.4782,12.0094 -2.0274,2.7255 -0.8976,4.0236 -2.9468,6.6201 -7.4257,9.4085 -15.5977,17.491 -19.5357,18.4377 -12.3448,2.9679 -31.022,13.3523 -48.8679,18.4487 -6.9563,1.9865 -11.8611,7.0591 -17.056,7.7883 -3.6233,0.5085 -9.1094,-1.3697 -11.257,-2.3352 -21.6527,-9.7345 -43.2176,-18.6147 -54.3964,-18.1593 -9.4545,0.3851 -17.9721,2.2025 -25.871,2.8683 -9.4418,0.7959 -17.6964,0.553 -21.4779,2.5173 -20.4822,10.6398 -38.419,16.8192 -48.8224,20.3494 -5.0142,1.7014 -20.5695,-0.1812 -24.0787,-5.0784 -4.6909,-6.5465 -9.156,-12.7122 -12.0311,-16.2997 -4.6996,-5.8642 -21.4343,-7.0447 -24.672,-11.8001 -7.9598,-11.6912 -11.9636,-13.068 -20.1826,-13.7417 -7.8354,-0.6421 -15.6424,-0.2842 -19.6629,-2.0745 -7.5632,-3.3677 -15.4619,-7.7216 -20.7261,-13.6823 -7.4688,-8.457 -15.0324,-15.4424 -20.7904,-15.8006 -20.248,-1.2599 -44.7202,-2.169 -50.3758,0.5859 -12.952,6.309 -26.4825,17.6083 -42.1478,29.5608 -35.2622,26.9047 -74.1758,60.7026 -86.1032,76.1249 -9.011,11.6512 -35.4481,20.5531 -52.4208,17.5993 -6.0932,-1.0604 -17.5877,-2.6336 -24.4035,-3.9203 -18.3328,-3.4609 -36.8443,-7.1342 -55.4427,-10.5856 -22.4561,-4.1674 -42.9395,-7.4163 -47.929,-8.927 -21.5851,-6.5354 -55.9162,-16.7875 -89.1005,-24.3913 -77.0311,-17.6509 -157.9488,-37.5469 -185.2752,-38.5747 -24.4428,-0.9194 -58.6903,-1.9461 -80.1877,0.097 -15.302,1.4542 -21.8852,6.1791 -24.1418,9.6414 -12.2517,18.7977 -30.0482,46.4049 -46.5119,72.2002 -6.449,10.1043 -14.1078,20.2842 -19.7344,29.1941 -12.5929,19.9414 -20.2501,34.7257 -22.9333,38.7873 -11.0826,16.7755 -23.1921,39.162 -29.5606,60.8938 -6.998,23.8797 -12.4568,44.3603 -16.4415,47.977 -24.2217,21.9844 -64.1565,58.0756 -74.2183,73.2005 -20.7597,31.206 -35.3537,59.9306 -46.3133,76.0739 -2.6229,3.8635 -0.7573,11.2386 -0.6542,16.6385 0.4418,23.1273 -2.3832,50.5585 1.1639,61.6403 4.822,15.0646 8.9944,29.6291 9.0491,34.8889 0.1349,12.9707 -1.6034,29.114 0.4771,44.8861 2.7665,20.9735 2.1305,39.745 6.4576,46.9632 9.6593,16.1129 15.0122,26.413 20.2385,36.8298 0,0 -1.9415,22.5419 -2.3372,29.1982 -0.5168,8.6919 -4.0658,13.3211 -5.8116,15.6685 -3.2767,4.4061 -9.0635,8.9647 -12.0823,14.5992 -2.8565,5.3315 -2.9755,11.7004 -5.0288,17.2764 -2.6924,7.3116 -4.4551,14.476 -4.6609,20.5553 -0.3221,9.5165 0.4495,20.0281 -1.5367,21.7543 -10.686,9.2868 -21.1662,20.0768 -24.2627,26.8799 -3.8864,8.5385 -3.5705,19.5691 -5.5232,28.681 m -7.7465,23.8742 c -4.3888,4.6664 -8.4683,9.5984 -11.2887,14.256 -6.2414,10.3069 -12.9209,22.7079 -11.9218,35.0995 1.1148,13.8261 3.2904,27.9456 -0.2664,38.2594 -4.3121,12.5041 -3.0859,20.3927 -4.8983,22.6338 -5.5473,6.8597 -15.3677,14.0368 -16.4822,18.8417 -3.1519,13.5889 -9.2743,25.7279 -2.1977,26.5284 6.6149,0.7483 12.975,3.7905 20.4546,4.4308 5.6923,0.4873 12.3903,-3.8845 17.9431,-4.0248 4.8549,-0.1226 8.6678,3.9889 12.3604,4.2539 5.8876,0.4225 9.4005,2.0181 11.7143,1.9073 17.5236,-0.8389 36.0365,-2.8798 41.7541,0.2445 7.1703,3.9183 14.3489,8.0916 18.4919,11.1891 7.8769,5.8892 13.1147,13.6893 21.1292,20.5709 m 18.0313,11.6673 c 3.6249,3.5772 4.6904,5.0345 6.2514,10.1693 7.2867,23.968 12.4102,57.3542 20.6973,66.1531 4.2646,4.528 20.2796,19.2077 30.6436,23.0903 13.6498,5.1134 24.7692,10.0194 26.5074,13.234 4.0454,7.4817 1.3183,15.139 5.7578,21.4685 4.3883,6.2565 3.5029,0.8731 3.4327,17.1088 -0.034,7.9526 -4.0142,19.1507 -4.6045,23.9153 -1.6783,13.5449 -5.6153,34.9673 -0.7242,50.887 3.7203,12.1088 14.821,21.7939 19.937,32.884 3.7774,8.1884 2.6673,18.2673 2.2017,20.8537 -2.8382,15.7666 -6.0967,31.365 -12.6819,45.6327 m -22.3632,111.3453 c -0.1782,2.2053 -11.394,-0.8364 -11.6639,1.4264 -3.1576,26.4794 5.9945,55.1301 3.5005,75.7265 l -20.7839,52.5088 -9.7628,41.1584 1.35,17.7353 c -0.772,10.7973 -1.3733,11.1467 -5.4501,17.3328 -4.2533,6.4538 -11.1988,10.7321 -16.1228,15.6497 -20.8839,18.4639 -11.6387,19.5456 -9.4785,24.7148 l 6.7643,11.5277 1.5717,36.3842 -6.7886,60.9305 -11.7417,32.0977 -13.7472,24.8094 -33.5542,34.0611 -2.7958,11.5912 -13.5667,14.2242 c -1.0183,6.9918 1.0546,14.4251 -5.9854,20.5567 l -45.1657,68.8199 -9.3395,24.9579 -12.1989,12.4168 c -3.187,9.6806 -8.2663,16.5327 -7.0482,26.2133 -0.8151,6.8957 -1.3685,14.5759 -3.0715,18.8078 l -41.2427,52.5471 c -7.3074,14.3396 -22.6421,37.3775 -14.4975,46.5542 11.3885,10.8723 20.5697,19.327 37.4711,26.524 l 34.7826,25.4759 c 2.2224,14.4666 6.9018,29.4247 4.8602,43.0385 -7.4861,6.3406 -20.6018,11.2738 -31.4605,16.7712 -7.8628,26.8592 -4.8214,50.2669 -0.6486,73.3983 l 34.1164,11.4699 8.6478,8.086 c 3.9573,0.01 11.4502,2.1041 15.7611,-0.5147 l 18.4854,0.4291 4.8725,4.3147 c 9.7831,0.01 6.8795,-0.7412 6.9325,-10.2045 2.6433,-11.9848 11.1863,-15.4478 22.2836,-15.2212 l 29.1321,8.7225 c 7.0803,3.3978 15.6326,5.2197 20.49,8.9136 17.8458,13.571 12.7541,9.7834 30.3325,6.3033 25.7309,5.4137 68.3007,11.95 77.1926,16.2411 7.6522,4.5919 12.4815,13.1361 24.3492,11.8265 l 56.2643,3.7759 c 9.9847,2.7017 19.4663,5.2741 23.8591,16.3636 l -1.2613,44.3667 -13.7539,51.9945 -16.8297,31.9794 -1.0797,26.0137 7.0003,26.9758 4.6947,51.0036 4.5948,10.7516 c 2.4195,4.9057 10.9106,9.6578 19.9677,9.9655 l 29.8085,-11.7205 40.8209,-28.9282 49.0999,-25.6863 c 11.3834,-2.7714 28.1916,7.5655 36.1544,15.7327 l 28.645,3.5588 18.803,2.892 26.0439,4.4267 13.0165,15.3725 11.3549,6.7785 26.2183,19.2408" /><circle id="1" /></g>';
xmlDoc = $.parseXML( xml );
$xml = $( xmlDoc );
sxx='';

//节点g遍历
$xml.children().each(function(){
//sxx = sxx + $(this).attr('id') + '--' + $(this).attr('onmousemove') + $(this).attr('onmouseout') + $(this).attr('style');
$.each(this.attributes,function(i,attrib){
//alert(attrib.name);
//alert(attrib.value);

sxx = sxx + attrib.name + ':' + attrib.value;
});

//遍历g的子节点,注意根据实际情况这里可以进行多次嵌套遍历子节点
$(this).children().each(function(){
$.each(this.attributes,function(i,attrib){
alert(attrib.name);
alert(attrib.value);
});
});
});

$("#output").html(sxx);

});
</script>
</head>
<body>
<div id="output">

</div>
</body>
</html>

经过jQuery1.5测试,源码在本人的资源中http://download.csdn.net/user/Pipi0714
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: