Diligent achievement genius ...

业精于勤荒于嬉 行成于思毁于随 voiow博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

javascript以list方式封装xml

Posted on 2010-01-04 23:52  voiow  阅读(237)  评论(0编辑  收藏  举报

//以List的方式封装xml
function getOneObjectByXML(xmlStr, methodPath) {
 var xmlDoc = null;
 //加载xml字符串
 if (window.ActiveXObject){
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  //var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
 } else {
  if (document.implementation&&document.implementation.createDocument){
   xmlDoc=document.implementation.createDocument("","doc",null);
  }
 }
 
 //设置xml对象的属性
 xmlDoc.async = false;
 xmlDoc.preserveWhiteSpace = true;
 
 //使用两种形式加载并获取加载是否成功
 var bol = xmlDoc.load(xmlStr) ? xmlDoc.load(xmlStr) : xmlDoc.loadXML(xmlStr);
  var nodes = xmlDoc.documentElement;
 
  //获取methodPath指定路径下的所有子节点
 var temp = xmlDoc.getElementsByTagName(methodPath)[0].childNodes;
 
 //循环获取所有字节对象(保存为Array)
 var objectList = new Array();
 for (i = 0; i < temp.length; i ++) {
  objectList[i] = getChildNode(temp[i].childNodes);
 }
 return objectList;
}

//辅助获取返回值递归调用
function getChildNode(sonNodeList) {
 var mySonList = new Array();
 var j = 0;
 for (; j < sonNodeList.length; j ++) {
  //判断该节点下面是否有子节点,如果有就循环子节点,否则就保存
  if (sonNodeList[j].childNodes[0].tagName != null) {
   mySonList[sonNodeList[j].tagName] = getChildNode(sonNodeList[j].childNodes);
  } else {
   if (sonNodeList[j].tagName == 'item')
    mySonList[j] = sonNodeList[j].text;
   else
    mySonList[sonNodeList[j].tagName] = sonNodeList[j].text;
  }
 }
 return mySonList;
}

//辅助SOAP构造多个同名子节点
//sonNodeName:子节点名称
//valueList:子节点值得列表Array对象
//makeValue:如果valueList为null,就根据makeValue来判断生成字符、数字、BIT。。。。。。(str/bit/num/char)
//sonCount:子节点个数
//sonNodeLength:字节点值得长度/多少范围内的整数
function createManySonNodes(sonNodeName, valueList, makeValue, sonCount, sonNodeLength) {
  var tempNodeList = "";
 if (valueList != null) {
  for (i = 0; i < valueList.length; i ++) {
   tempNodeList += "<" + sonNodeName + ">" + valueList[i] + "</" + sonNodeName + ">";
  }
 } else {
  if (makeValue == 'str') {
   for (i = 0; i < sonCount; i ++) {
    tempNodeList += "<" + sonNodeName + ">" + randomStr(sonNodeLength) + "</" + sonNodeName + ">";
   }
  } else if (makeValue == 'bit') {
   for (i = 0; i < sonCount; i ++) {
    tempNodeList += "<" + sonNodeName + ">" + randomBit() + "</" + sonNodeName + ">"
   }
  } else if (makeValue == 'num') {
   for (i = 0; i < sonCount; i ++) {
    tempNodeList += "<" + sonNodeName + ">" + randomNumber(sonNodeLength) + "</" + sonNodeName + ">"
   }
  } else if (makeValue == 'char') {
   for (i = 0; i < sonCount; i ++) {
    tempNodeList += "<" + sonNodeName + ">" + randomChar(sonNodeLength) + "</" + sonNodeName + ">"
   }
  }
 }
 return tempNodeList;
}

TrackURL:http://janhang.blog.hexun.com/25233469_d.html