标题:从远程webservice返回的xml就是解析不了呢?
取消只看楼主
bobingpzm
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-7-18
 问题点数:0 回复次数:0 
从远程webservice返回的xml就是解析不了呢?
我从远程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 "";  
  }
搜索更多相关主题的帖子: 解析xml 
2008-08-29 21:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-230511-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.430811 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved