#2
quanercao2008-10-06 00:03
|
我从远程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://
<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></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 "";
}