注册 登录
编程论坛 jQuery论坛

从远程webservice返回的xml就是解析不了呢?

bobingpzm 发布于 2008-08-29 21:20, 3544 次点击
我从远程webservice返回的xml如下
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas. xmlns:xsi="http://www. xmlns:xsd="http://www. xmlns="http://
  &lt;HerosNames&gt;
    &lt;user_name&gt;456&lt;/user_name&gt;
    &lt;user_alias&gt;123&lt;/user_alias&gt;
    &lt;user_no&gt;9&lt;/user_no&gt;
  &lt;/HerosNames&gt;
  &lt;HerosNames&gt;
    &lt;user_name&gt;123&lt;/user_name&gt;
    &lt;user_alias&gt;456&lt;/user_alias&gt;
    &lt;user_no&gt;11&lt;/user_no&gt;
  &lt;/HerosNames&gt;
  &lt;HerosNames&gt;
    &lt;user_name&gt;123&lt;/user_name&gt;
    &lt;user_alias&gt;456&lt;/user_alias&gt;
    &lt;user_no&gt;12&lt;/user_no&gt;
  &lt;/HerosNames&gt;
  &lt;HerosNames&gt;
    &lt;user_name&gt;ren&lt;/user_name&gt;
    &lt;user_alias&gt;WICRESOFT\i-fengl&lt;/user_alias&gt;
    &lt;user_no&gt;2&lt;/user_no&gt;
  &lt;/HerosNames&gt;
  &lt;HerosNames&gt;
    &lt;user_name&gt;Peng Li&lt;/user_name&gt;
    &lt;user_alias&gt;WICRESOFT\i-pengli&lt;/user_alias&gt;
    &lt;user_no&gt;3&lt;/user_no&gt;
  &lt;/HerosNames&gt;
  &lt;HerosNames&gt;
    &lt;user_name&gt;Sweet Xue&lt;/user_name&gt;
    &lt;user_alias&gt;WICRESOFT\i-sweetx&lt;/user_alias&gt;
    &lt;user_no&gt;4&lt;/user_no&gt;
  &lt;/HerosNames&gt;
&lt;/NewDataSet&gt;</GetHeroNameResult></GetHeroNameResponse></soap:Body></soap:Envelope>
我本地webservice返回的xml如下
<NewDataSet> <HerosNames> <user_name>456</user_name> <user_alias>123</user_alias> <user_no>9</user_no> </HerosNames> <HerosNames> <user_name>123</user_name> <user_alias>456</user_alias> <user_no>11</user_no> </HerosNames> <HerosNames> <user_name>123</user_name> <user_alias>456</user_alias> <user_no>12</user_no> </HerosNames> <HerosNames> <user_name>ren</user_name> <user_alias>WICRESOFT\i-fengl</user_alias> <user_no>2</user_no> </HerosNames> <HerosNames> <user_name>Peng Li</user_name> <user_alias>WICRESOFT\i-pengli</user_alias> <user_no>3</user_no> </HerosNames> <HerosNames> <user_name>Sweet Xue</user_name> <user_alias>WICRESOFT\i-sweetx</user_alias> <user_no>4</user_no> </HerosNames> </NewDataSet>
用同样的方式解析但是从远程webservice返回的xml就是解析不了呢?解析方法如下
list = getXMLArray(xmlDoc,'HerosNames');
obj = list[i]
getValue(obj,'user_alias')
function getXMLArray(xmlDoc, name)
   {  
      var keys = name.split('.');  
      var node = xmlDoc.documentElement;      // 得到根节点  
      var rtn = new Array();  
      var n = 0;  
      var i;
      var k;
      for(i=0; i<keys.length; i++)
      {  
          var childs = node.childNodes;       // 得到子节点  
          var key = keys[i];  
          for(k=0; k<childs.length; k++)
           {        
           var child = childs[k];  
           if(child.nodeName == key)
               {     // 判断子节点是否符合  
                  if(i == keys.length-1)
                    {  
                      rtn[n] = child;  
                      n++;  
                    }
                 else
                    {  
                      node = child;  
                     break;  
                    }  
               }  
           }  
      }  
      return rtn;  
   }
   
function getValue(node, name)
  {  
      var keys = name.split('.');  
      for(var i=0; i<keys.length; i++)
      {  
        var childs = node.childNodes;       // 得到子节点  
        var key = keys[i];  
        for(var k=0; k<childs.length; k++)
        {        
          var child = childs[k];  
          if(child.nodeName == key)
          {     // 判断子节点是否符合  
            if(child.childNodes.length == 1)
               {           
                 // 如果没有字节点,返回值  
                 return child.text;  
               }
              else
              {                                   
                // 还有子节点,继续分析  
                node = child;  
                break;  
              }  
           }  
         }  
       }  
       return "";  
  }
1 回复
#2
quanercao2008-10-06 00:03
据说。。大侠的webservice返回的xml格式不对。。远程调用时服务器帮你补充完整了。。
可以试着使webservice返回xmldocument对象。。这时解析就不会出问题了。。
1