求高手指导 js 调用webservice 以及xml解析问题 用的是mt.js (不会的捧个人场)

hcr1987 2011-02-21 10:19:23
利用mt 和自己写的js(百度的- -) 访问webservice 返回的xml对象一直是null 在这方面小菜 求帮组 代码如下
[code=HTM]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ly3.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="js/mootools.v1.11.js" type="text/javascript"></script>
<script src="js/WebService.js" type="text/javascript"></script>

<script type="text/javascript">

var service ;
function ajaxRequest()
{

var url = "http://futstar.wlwlw.net/NodeService.asmx";
//http://futstar.wlwlw.net/NodeService.asmx?op=GetAllNodeErrorDevices
//设置webService传入参数
//
//注意:
//
// 调用webservice(如例子中的webservicedemo.asmx)
// HelloTo(String name) 针对name参数必须写成name=wqj ,还有更多参数一样写,使用&符号分隔(name=11&age=20&.....),使用名称匹配
// 传入的参数数量可以不等于(多于或少于)方法要求的参数

var para = "areaId=8";

var op = {
data:para,
onComplete: showResponse,
onFailure:showError,
update:'ajaxBack'
};

service = new WebService(url,"GetAllNodes",op);
service.request();
return false;
}
function showError(obj)
{
//obj 是一个xmlHttpRequest对象
alert("error");
}
function showResponse(requestText,requestXML)
{
//requestText
//requestXML
//requestText 返回的文本
//requestXML 返回的XML
// service.getBackData 就是取出返回的XML中,实际需要的数据
//经过测试兼容 IE,FF
alert(service.getBackData(requestXML)+"html");
var c = requestText;
var b = requestXML;

alert(b);

}
</script>

</head>

<body background="../jiaju.jpg" onLoad="ajaxRequest()">
<div >
<img id="hcr" src="img/1.png" onMouseOver="this.src='img/2.png';" onmouseout="this.src='img/1.png'" />
</div>
<div id="ajaxBack">uhiuiui</div>
</body>
</html>

[/code]

webservice.js的代码
[code=JScrip]

var WSDLS = {};

var WebService = new Class({

url : '',
method : '',
options:
{
method:'GET',
data: null,
update: null,
onComplete: Class.empty,
onError:Class.empty,
evalScripts: false,
evalResponse: false
},

initialize: function(url,method,options)
{
this.url = url;
this.method = method;
this.options = options;
},

request : function()
{
var wsdl = WSDLS[this.url];
if(!wsdl)
{
var op = {method:'GET',async: false};
var wsdlAjax = new XHR(op).send(this.url + "?wsdl", null);
wsdl = wsdlAjax.transport.responseXML;
WSDLS[this.url] = wsdl;
}

this.setSoap(wsdl);
},

setSoap : function(wsdl)
{
var paraXML = this.getParaXML(wsdl);
alert(paraXML);
var ns = (wsdl.documentElement.attributes["targetNamespace"] + "" == "undefined") ? wsdl.documentElement.attributes.getNamedItem("targetNamespace").nodeValue : wsdl.documentElement.attributes["targetNamespace"].value;
var sr =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soap:Body>" +
"<" + this.method + " xmlns=\"" + ns + "\">" +
paraXML +
"</" + this.method + "></soap:Body></soap:Envelope>";

this.options.method = 'post';
this.options.data = null;

var soapaction = ((ns.lastIndexOf("/") != ns.length - 1) ? ns + "/" : ns) + this.method;

var soapAjax = new Ajax(this.url,this.options);
soapAjax.setHeader("SOAPAction", soapaction);
soapAjax.setHeader("Content-type", "text/xml; charset=utf-8");
soapAjax.request(sr);
},
getParaXML : function(wsdl)
{

var objNode = null;
var rtnValue = "";
//java(xfire)
var ell = this.getElementsByTagName(wsdl,"xsd:element");
if(ell.length == 0)
{
//c#
ell = this.getElementsByTagName(wsdl,"s:element");
}
for(var i = 0; i < ell.length; i++)
{
if(this.getElementAttrValue(ell[i],"name") == this.method)
{
objNode = ell[i];
break;
}
}

if(objNode == null) return rtnValue;
//java(xfire)
ell = this.getElementsByTagName(objNode,"xsd:element");
if(ell.length == 0)
{
//c#
ell = this.getElementsByTagName(objNode,"s:element");
}
if(ell.length == 0) return rtnValue ;

var hash = new Hash();

if(this.options.data != null && this.options.data.clean != "")
{
hash = this.options.data.split("&").toHash("=");
}

for(var i = 0; i < ell.length; i++)
{
var paraName = this.getElementAttrValue(ell[i],"name");
rtnValue = rtnValue + this.getSingleXML(paraName,hash);
}

return rtnValue;
},

getSingleXML : function (name,hash)
{
name = name.trim();

var rtnValue = "";
if(hash.hasKey(name))
{
rtnValue = hash.get(name);
}
rtnValue = "<" + name + ">" + xmlscc(rtnValue) + "</" + name + ">"
return rtnValue;
},
getBackData: function(xml)
{
var rtnValue = "";
//java(xfire)
var soap = this.getElementsByTagName(xml,"ns1:out");
if(soap.length == 0)
{
//c#
soap = this.getElementsByTagName(xml,this.method + "Result");
}
return soap[0].childNodes[0].nodeValue;

},
getElementsByTagName : function(objNode,tagName)
{
//tagName 形式如 xsd:element ,写出tag的全称

var ell;
if(this.isIE())
{
ell = objNode.getElementsByTagName(tagName);
}
else
{
if(tagName.contains(":")) tagName = tagName.split(":")[1];
ell = objNode.getElementsByTagName(tagName);
}
return ell;
},
getElementAttrValue : function(objNode,attrName)
{
var rtnValue = "";

if(objNode == null) return rtnValue;

if(objNode.attributes[attrName] + "" == "undefined")
{
if(objNode.attributes.getNamedItem(attrName) != null)
rtnValue = objNode.attributes.getNamedItem(attrName).nodeValue ;

}
else
{
if(objNode.attributes[attrName] != null)
rtnValue = objNode.attributes[attrName].value;
}
return rtnValue;
},
isIE : function()
{
var isMSIE = /*@cc_on!@*/false;
return isMSIE;
}
});

Array.extend({

toHash : function (splitChar)
{
var hash = new Hash({});
for(var i=0;i<this.length;i++)
{

if(this[i].split(splitChar).length == 1) contrnue;

var key = this[i].split(splitChar)[0].trim();
var value = this[i].split(splitChar)[1].trim();

hash.set(key, value);
}

return hash;
}
});

function xmlscc(strData)
{

strData=strData.replace(/&/g, "&");
strData=strData.replace(/>/g, ">");
strData=strData.replace(/</g, "<");
strData=strData.replace(/"/g, """);
strData=strData.replace(/'/g, "'");
return strData;
}
[/code]

返回效果service.getBackData 是null
responseXML 是object 就是想解析他 得到子节点之类的 跟id标示
responseText 返回如下



如果有别的方法调用 "http://futstar.wlwlw.net/NodeService.asmx"也行 请大家指教
...全文
280 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcr1987 2011-02-22
  • 打赏
  • 举报
回复
[color=#FF0000][/- -无视他吧 我换个思路呗 我在页面取出值传给后台再处理吧- -无奈 他老是提示我拒绝访问 是跨域问题吗?怎么解决color]
kai27ks 2011-02-22
  • 打赏
  • 举报
回复
晕。。楼主应该贴关键代码 大家一个一个找。你这代码实在太多了。别人没耐心看下去的
hcr1987 2011-02-22
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?><soap xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap><GetAllNodesResponse xmlns="http://tempuri.org/"><GetAllNodesResult><anyType xsi:type="node"><id>91</id><area_id>8</area_id><node_id>43</node_id><node_name>红外</node_name><node_x>452</node_x><node_y>470</node_y><node_tyleid>15</node_tyleid><dev_id>3</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>92</id><area_id>8</area_id><node_id>48</node_id><node_name>温湿度</node_name><node_x>594</node_x><node_y>408</node_y><node_tyleid>2</node_tyleid><dev_id>2</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>93</id><area_id>8</area_id><node_id>34</node_id><node_name>光照</node_name><node_x>612</node_x><node_y>486</node_y><node_tyleid>18</node_tyleid><dev_id>7</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>94</id><area_id>8</area_id><node_id>47</node_id><node_name>温湿度</node_name><node_x>738</node_x><node_y>402</node_y><node_tyleid>2</node_tyleid><dev_id>4</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>95</id><area_id>8</area_id><node_id>33</node_id><node_name>光照</node_name><node_x>710</node_x><node_y>487</node_y><node_tyleid>16</node_tyleid><dev_id>5</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>96</id><area_id>8</area_id><node_id>32</node_id><node_name>光照</node_name><node_x>798</node_x><node_y>700</node_y><node_tyleid>20</node_tyleid><dev_id>1</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>97</id><area_id>8</area_id><node_id>46</node_id><node_name>温湿度</node_name><node_x>457</node_x><node_y>698</node_y><node_tyleid>2</node_tyleid><dev_id>2</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>98</id><area_id>8</area_id><node_id>31</node_id><node_name>光照</node_name><node_x>857</node_x><node_y>404</node_y><node_tyleid>16</node_tyleid><dev_id>8</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>99</id><area_id>8</area_id><node_id>36</node_id><node_name>雨滴</node_name><node_x>701</node_x><node_y>337</node_y><node_tyleid>17</node_tyleid><dev_id>6</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>100</id><area_id>8</area_id><node_id>35</node_id><node_name>风速</node_name><node_x>1062</node_x><node_y>307</node_y><node_tyleid>11</node_tyleid><dev_id>2</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>101</id><area_id>8</area_id><node_id>37</node_id><node_name>雨滴</node_name><node_x>924</node_x><node_y>336</node_y><node_tyleid>12</node_tyleid><dev_id>6</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>105</id><area_id>8</area_id><node_id>41</node_id><node_name>光照</node_name><node_x>193</node_x><node_y>672</node_y><node_tyleid>19</node_tyleid><dev_id>5</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>106</id><area_id>8</area_id><node_id>42</node_id><node_name>光照</node_name><node_x>2222</node_x><node_y>132</node_y><node_tyleid>10</node_tyleid><dev_id>5</dev_id><node_netTime>20</node_netTime></anyType><anyType xsi:type="node"><id>109</id><area_id>8</area_id><node_id>49</node_id><node_name>温湿度</node_name><node_x>1897</node_x><node_y>340</node_y><node_tyleid>22</node_tyleid><dev_id>2</dev_id><node_netTime>20</node_netTime></anyType></GetAllNodesResult></GetAllNodesResponse></soap></soap>
soap返回的数据处理不了~头疼 ActiveXObject 对2个soap带命名空间的好像解析不了alert 长度一直出不出来
jim_write 2011-02-22
  • 打赏
  • 举报
回复
原来你发了3个这帖子 继续接分 回复一次抵3次
hcr1987 2011-02-22
  • 打赏
  • 举报
回复
明早给分 回复的都有 谢谢啊
hcr1987 2011-02-22
  • 打赏
  • 举报
回复
算了呗- - 我用服务写吧 自己想多了 给分咯
hcr1987 2011-02-21
  • 打赏
  • 举报
回复
好诶 我先看看
  • 打赏
  • 举报
回复
javascript调用webservice示例


http://www.javaeye.com/topic/98182


里面有相关包下载
  • 打赏
  • 举报
回复
解析XML

CSDN 上有人提过解析XML的问题,你可以参考一下。
http://topic.csdn.net/u/20101217/21/8e9c3169-248a-439c-84c6-5d5a3a9763c9.html?1885069617


另外:
Android 支持JAVA传统的两种方式:DOM 和SAX。

DOM 需要load整个文件,所以需要比较多的内存,担正因为此,整颗树都生成好,比较直观。

DOM的代码稍微复杂些,以下是几个准则:

· 整个文档是一个文档节点

· 每个 XML 标签是一个元素节点(element)

· 包含在 XML 元素中的文本是文本节点(text)

· 每一个 XML 属性是一个属性节点 (attribute)

· 注释属于注释节点

关键是得到一个Document对象,并得到对应的层层Node。(element、attribute、textnode都是node。其中element是比较直观理解的元素)。

SAX 的优点是选择性的parse 某个节点,比较方便。

关键是自己实现一个org.xml.sax.helpers.DefaultHandler的基础类,并重载:

startDocument 打开一个文档的callback

endDocument 关闭一个文档的callback

startElement 打开一个元素的callback

endElement 关闭一个元素的callback

characters 读到一个元素的内容的callback (注:这里没有什么文本节点的概念了,只有元素的概念)

在这个函数里面,可以选择性的读取某些元素的内容

例子:

<?xml version="1.0"?>
<books>
<book category="computer-programming">
<author>Steven Haines</author>
<title>Java 2 Primer Plus</title>
<price>44.95</price>
</book>
<book category="fiction">
<author>Tim LaHaye</author>
<title>Left Behind</title>
<price>15.95</price>
</book>
</books>

两种方法的parse:

DOM:

String parseXMLDom(String filename)
{
String result = null;
String temp;
try
{
File file = new File( "/sdcard/book.xml" );
if( !file.exists() )
{
String st = String.format( "Couldn't find file..." );
return st;
}

// Parse the document
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( file );

// Walk the document
Element root = document.getDocumentElement();
temp =( "root=" + root.getTagName()+"\n" );
result += temp;
// List the children of <books>; a set of <book> elements
NodeList list = root.getChildNodes();
for( int i=0; i<list.getLength(); i++ )
{
Node node = list.item( i );
if( node.getNodeType() == Node.ELEMENT_NODE )
{
// Found a <book> element
temp = ( "Handling node: " +
node.getNodeName()+"\n" );

result += temp;

Element element = ( Element )node;
temp = ( "tCategory Attribute: " +
element.getAttribute( "category" )+"\n" );

result += temp;

// Get its children: <author>, <title>, <price>
NodeList childList = element.getChildNodes();
for( int j=0; j<childList.getLength(); j++ )
{
// Once one of these nodes is attained, the next
// step is locating its text element
// It is important. It is NOT <author>'s value, but it is its text element's value
Node childNode = childList.item( j );
if( childNode.getNodeType() == childNode.ELEMENT_NODE )
{
NodeList childNodeList =
childNode.getChildNodes();
for( int k=0; k<childNodeList.getLength(); k++ )
{
Node innerChildNode = childNodeList.item( k );

temp = ( "ttNode (" + childNode.getNodeName()+ ") = "+
innerChildNode.getNodeValue() +"\n");
result += temp;

//innerChildNode's name is "text", and childNode's value is null
}
}
}
}
}




} catch( Exception e )
{
e.printStackTrace();
}


return result;

}

SAX:

String parseSAX(String filename)
{
String result = "";

File file = new File( "/sdcard/book.xml" );
if( !file.exists() )
{
String st = String.format( "Couldn't find file..." );
return st;
}

FileInputStream read;
try {
read = new FileInputStream(file);



SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();


XMLReader xr = sp.getXMLReader();

ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);


xr.parse(new InputSource(read));


result = myExampleHandler.getParsedData();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

public class ExampleHandler extends DefaultHandler {

String result = "";
int index = -1;

String category[] = new String [2];
String author[] = new String [2];
String title[] = new String [2];
String price[] = new String [2];

private boolean bbook = false;
private boolean bprice = false;
private boolean bauthor = false;
private boolean btitle = false;

public String getParsedData() {
return this.result;
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if(bbook && bprice)
price[index] = new String(ch, start, length)+ "\n";
if(bbook && bauthor)
author[index] = new String(ch, start, length)+ "\n";
if(bbook && btitle)
title[index] = new String(ch, start, length)+ "\n";
super.characters(ch, start, length);
}

@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
int i;
for(i=0; i<=index; i++){
result += "Book: \n";
result += title[i];
result += category[i];
result += author[i];
result += price[i];

}
super.endDocument();
}

@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// TODO Auto-generated method stub

if (localName.equals("book")) {
bbook = false;

} else if (localName.equals("author")){
bauthor = false;
}else if (localName.equals("title")){
btitle = false;
}else if (localName.equals("price")){
bprice = false;
}

super.endElement(uri, localName, name);
}

@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
index = -1;
super.startDocument();
}

@Override
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("book")) {
bbook = true;
index ++;
String attrValue = atts.getValue("category");
category[index]= attrValue + "\n";

} else if (localName.equals("author")){
bauthor = true;
}else if (localName.equals("title")){
btitle = true;
}else if (localName.equals("price")){
bprice = true;
}


super.startElement(uri, localName, name, atts);
}

}





希望对你有帮助
hcr1987 2011-02-21
  • 打赏
  • 举报
回复
自己顶自己 在线等
hcr1987 2011-02-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 woshigaoshou980 的回复:]
javascript调用webservice示例


http://www.javaeye.com/topic/98182


里面有相关包下载
[/Quote]
我用的就他的方法- -
function showResponse(requestText,requestXML)
{
//requestText
//requestXML
//requestText 返回的文本
//requestXML 返回的XML
// service.getBackData 就是取出返回的XML中,实际需要的数据
//经过测试兼容 IE,FF
alert(service.getBackData(requestXML)+"html");
var c = requestText;
var b = requestXML;

alert(c);

}
在则合格方法中service.getBackData 返回时Null 他的js看的我头晕 菜了- - 所以求指导下

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧