sspachtholz
asked on
Help parsing an XML response from a SOAP server
I've created a page that now sends off a proper request to a SOAP server and gets a response back. The SOAP request is to log into the system. The response has a token code in the XML that I need to use in all subsequent requests.
Today, the page displays the entire response as text on the web page. What I need to do next is parse the response and store the token as a variable.
I've read some online tutorials and tried to hack together some code. But its just not working.
In the code below, the submit button calls the makeSoapRequest() function. That function then calls the parseXMLDocument() function. This new parseXMLDocument() function is where I am having problems.
Any assistance would be greatly appreciated.
----Here is the code for the page--------
<html>
<head>
<script language="javascript">
function makeSoapRequest(form){
// create the payload of the SOAP request
var soapData = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://XXXXX/jboss-net/services/sylantro/auth" xmlns:intf="http://XXXXX/jboss-net/services/sylantro/auth" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://webapi.sylantro.com/webapi" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \n' +
'<SOAP-ENV:Body> \n' +
'<mns:loginUser xmlns:mns="http://XXXXX/jboss-net/services/sylantro/auth" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> \n' +
'<in0 xsi:type="tns1:EULoginRequ est"> \n' +
'<applicationName xsi:type="xsd:string">USER PORTAL</ap plicationN ame> \n' +
'<tenantName xsi:type="xsd:string" /> \n' +
'<userName xsi:type="xsd:string">' + form.username.value + '</userName> \n' +
'<userPassword xsi:type="xsd:string">' + form.password.value + '</userPassword> \n' +
'</in0> \n' +
'</mns:loginUser> \n' +
'</SOAP-ENV:Body> \n' +
'</SOAP-ENV:Envelope> \n';
//alert(soapData);
try{
netscape.security.Privileg eManager.e nablePrivi lege("Univ ersalBrows erRead");
}catch (e) {
//alert("Permission UniversalBrowserRead denied.");
};
try{
if (window.XMLHttpRequest){
req = new XMLHttpRequest();
}else{
req = new ActiveXObject("Microsoft.X MLHTTP");
}
}catch(e){
};
req.open("POST", "http://XXXXX/jboss-net/services/sylantro/auth", false);
req.setRequestHeader("Cont ent-Length ", soapData.length);
req.setRequestHeader("Cont ent-Type", "text/xml");
req.setRequestHeader("Soap action", "sylantro/auth");
req.send(soapData);
document.getElementById('r esponse'). innerHTML = req.responseText;
var xmlDocument1 = req.responseXML;
parseXmlDocument(xmlDocume nt1);
}
function parseXmlDocument(docName){
var xmlDoc = new ActiveXObject("Microsoft.X MLDOM");
xmlDoc.async = "false";
xmlDoc.load(docName);
alert(xmlDoc.DocumentEleme nt.firstch ild;);
var tokencode = xmlDoc.DocumentElement.fir stchild;
};
</script>
</head>
<body>
<FORM NAME="myform" ACTION="" METHOD="GET">
<p>Username: <INPUT TYPE="text" NAME="username" VALUE=""></p>
<p>Password: <INPUT TYPE="text" NAME="password" VALUE=""></p>
<INPUT TYPE="button" NAME="submit" Value="Submit" onClick="makeSoapRequest(t his.form)" >
</FORM>
<br><hr><br>
<div align="left">
<span id="response"></span>
</div>
</body>
</html>
----This is what the SOAP response looks like that I need to parse-----
<?xml version="1.0" encoding="UTF-8" ?>
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <ns1:loginUserResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://XXXXX/jboss-net/services/sylantro/auth">
- <loginUserReturn xsi:type="ns2:Authenticati onInfo" xmlns:ns2="http://webapi.sylantro.com/webapi">
<locale xsi:type="xsd:string">en</ locale>
<token xsi:type="xsd:string">6181 3599c32032 fa5cb3c24f 6ab74902b1 c41d257b0f c97494f7a9 1028731eb5 2fef368934 b412cd3b7f 49fbe47877 f8f7f072e7 d37609abc5 bc3c2c3b69 81cc4d2f6e 0789d6341e 3a8d0d875e 5a5773ebca 32dda25298 a</token>
<userType xsi:type="xsd:int">1</user Type>
</loginUserReturn>
</ns1:loginUserResponse>
</soapenv:Body>
</soapenv:Envelope>
Today, the page displays the entire response as text on the web page. What I need to do next is parse the response and store the token as a variable.
I've read some online tutorials and tried to hack together some code. But its just not working.
In the code below, the submit button calls the makeSoapRequest() function. That function then calls the parseXMLDocument() function. This new parseXMLDocument() function is where I am having problems.
Any assistance would be greatly appreciated.
----Here is the code for the page--------
<html>
<head>
<script language="javascript">
function makeSoapRequest(form){
// create the payload of the SOAP request
var soapData = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://XXXXX/jboss-net/services/sylantro/auth" xmlns:intf="http://XXXXX/jboss-net/services/sylantro/auth" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://webapi.sylantro.com/webapi" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \n' +
'<SOAP-ENV:Body> \n' +
'<mns:loginUser xmlns:mns="http://XXXXX/jboss-net/services/sylantro/auth" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
'<in0 xsi:type="tns1:EULoginRequ
'<applicationName xsi:type="xsd:string">USER
'<tenantName xsi:type="xsd:string" /> \n' +
'<userName xsi:type="xsd:string">' + form.username.value + '</userName> \n' +
'<userPassword xsi:type="xsd:string">' + form.password.value + '</userPassword> \n' +
'</in0> \n' +
'</mns:loginUser> \n' +
'</SOAP-ENV:Body> \n' +
'</SOAP-ENV:Envelope> \n';
//alert(soapData);
try{
netscape.security.Privileg
}catch (e) {
//alert("Permission UniversalBrowserRead denied.");
};
try{
if (window.XMLHttpRequest){
req = new XMLHttpRequest();
}else{
req = new ActiveXObject("Microsoft.X
}
}catch(e){
};
req.open("POST", "http://XXXXX/jboss-net/services/sylantro/auth", false);
req.setRequestHeader("Cont
req.setRequestHeader("Cont
req.setRequestHeader("Soap
req.send(soapData);
document.getElementById('r
var xmlDocument1 = req.responseXML;
parseXmlDocument(xmlDocume
}
function parseXmlDocument(docName){
var xmlDoc = new ActiveXObject("Microsoft.X
xmlDoc.async = "false";
xmlDoc.load(docName);
alert(xmlDoc.DocumentEleme
var tokencode = xmlDoc.DocumentElement.fir
};
</script>
</head>
<body>
<FORM NAME="myform" ACTION="" METHOD="GET">
<p>Username: <INPUT TYPE="text" NAME="username" VALUE=""></p>
<p>Password: <INPUT TYPE="text" NAME="password" VALUE=""></p>
<INPUT TYPE="button" NAME="submit" Value="Submit" onClick="makeSoapRequest(t
</FORM>
<br><hr><br>
<div align="left">
<span id="response"></span>
</div>
</body>
</html>
----This is what the SOAP response looks like that I need to parse-----
<?xml version="1.0" encoding="UTF-8" ?>
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <ns1:loginUserResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://XXXXX/jboss-net/services/sylantro/auth">
- <loginUserReturn xsi:type="ns2:Authenticati
<locale xsi:type="xsd:string">en</
<token xsi:type="xsd:string">6181
<userType xsi:type="xsd:int">1</user
</loginUserReturn>
</ns1:loginUserResponse>
</soapenv:Body>
</soapenv:Envelope>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry - I mean:
var strToken = xmlDoc.getElementsByTagNam e('token') [0].text;
alert(strToken);
var strToken = xmlDoc.getElementsByTagNam
alert(strToken);
ASKER
It appears to be .text from what I've found. However, I tried value and NodeValue as well, just to be thorough.
I'm still getting at error at this line saying "Object doesn't support this property or method."
var token = xmlDoc.getElementByTagName ("token"). value;
Here is the complete code as it is now.
<html>
<head>
<script language="javascript">
function makeSoapRequest(form){
// create the payload of the SOAP request
var soapData = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://voiceplus.penntele.com/jboss-net/services/sylantro/auth" xmlns:intf="http://voiceplus.penntele.com/jboss-net/services/sylantro/auth" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://webapi.sylantro.com/webapi" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \n' +
'<SOAP-ENV:Body> \n' +
'<mns:loginUser xmlns:mns="http://voiceplus.penntele.com/jboss-net/services/sylantro/auth" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> \n' +
'<in0 xsi:type="tns1:EULoginRequ est"> \n' +
'<applicationName xsi:type="xsd:string">USER PORTAL</ap plicationN ame> \n' +
'<tenantName xsi:type="xsd:string" /> \n' +
'<userName xsi:type="xsd:string">' + form.username.value + '</userName> \n' +
'<userPassword xsi:type="xsd:string">' + form.password.value + '</userPassword> \n' +
'</in0> \n' +
'</mns:loginUser> \n' +
'</SOAP-ENV:Body> \n' +
'</SOAP-ENV:Envelope> \n';
//alert(soapData);
try{
netscape.security.Privileg eManager.e nablePrivi lege("Univ ersalBrows erRead");
}catch(e){
//alert("Permission UniversalBrowserRead denied.");
};
try{
if (window.XMLHttpRequest){
req = new XMLHttpRequest();
}else{
req = new ActiveXObject("Microsoft.X MLHTTP");
}
}catch(e){
};
req.open("POST", "http://voiceplus.penntele.com/jboss-net/services/sylantro/auth", false);
req.setRequestHeader("Cont ent-Length ", soapData.length);
req.setRequestHeader("Cont ent-Type", "text/xml");
req.setRequestHeader("Soap action", "sylantro/auth");
req.send(soapData);
document.getElementById('r esponse'). innerHTML = req.responseText;
var xmlDocument1 = req.responseXML;
parseXmlDocument(xmlDocume nt1);
}
function parseXmlDocument(docName){
var xmlDoc = new ActiveXObject("Microsoft.X MLDOM");
xmlDoc.async = "false";
xmlDoc.load(docName);
var token = xmlDoc.getElementByTagName ("token"). text;
alert(token);
};
</script>
</head>
<body>
<form name="myform" action="" method="GET">
<p>Username: <input type="text" name="username" value=""></p>
<p>Password: <input type="text" name="password" value=""></p>
<input type="button" name="submit" value="Submit" onClick="makeSoapRequest(t his.form)" >
</form>
<br><hr><br>
<div align="left">
<span id="response"></span>
</div>
</body>
</html>
I'm still getting at error at this line saying "Object doesn't support this property or method."
var token = xmlDoc.getElementByTagName
Here is the complete code as it is now.
<html>
<head>
<script language="javascript">
function makeSoapRequest(form){
// create the payload of the SOAP request
var soapData = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://voiceplus.penntele.com/jboss-net/services/sylantro/auth" xmlns:intf="http://voiceplus.penntele.com/jboss-net/services/sylantro/auth" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://webapi.sylantro.com/webapi" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \n' +
'<SOAP-ENV:Body> \n' +
'<mns:loginUser xmlns:mns="http://voiceplus.penntele.com/jboss-net/services/sylantro/auth" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
'<in0 xsi:type="tns1:EULoginRequ
'<applicationName xsi:type="xsd:string">USER
'<tenantName xsi:type="xsd:string" /> \n' +
'<userName xsi:type="xsd:string">' + form.username.value + '</userName> \n' +
'<userPassword xsi:type="xsd:string">' + form.password.value + '</userPassword> \n' +
'</in0> \n' +
'</mns:loginUser> \n' +
'</SOAP-ENV:Body> \n' +
'</SOAP-ENV:Envelope> \n';
//alert(soapData);
try{
netscape.security.Privileg
}catch(e){
//alert("Permission UniversalBrowserRead denied.");
};
try{
if (window.XMLHttpRequest){
req = new XMLHttpRequest();
}else{
req = new ActiveXObject("Microsoft.X
}
}catch(e){
};
req.open("POST", "http://voiceplus.penntele.com/jboss-net/services/sylantro/auth", false);
req.setRequestHeader("Cont
req.setRequestHeader("Cont
req.setRequestHeader("Soap
req.send(soapData);
document.getElementById('r
var xmlDocument1 = req.responseXML;
parseXmlDocument(xmlDocume
}
function parseXmlDocument(docName){
var xmlDoc = new ActiveXObject("Microsoft.X
xmlDoc.async = "false";
xmlDoc.load(docName);
var token = xmlDoc.getElementByTagName
alert(token);
};
</script>
</head>
<body>
<form name="myform" action="" method="GET">
<p>Username: <input type="text" name="username" value=""></p>
<p>Password: <input type="text" name="password" value=""></p>
<input type="button" name="submit" value="Submit" onClick="makeSoapRequest(t
</form>
<br><hr><br>
<div align="left">
<span id="response"></span>
</div>
</body>
</html>
ASKER
Flipz, that worked well.
Thanks for the guys.
Thanks for the guys.
No problem sspachtholz - glad to help. And thanks to you - the points you awarded me just put me over the 150,000 total - thanks for making me a Guru overall!!
Like nodeValue or text or value - I cannot remember off the top of my head