This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Conversion of Raw Soap Response Into HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Conversion of Raw Soap Response Into HTML" Watch "Conversion of Raw Soap Response Into HTML" New topic
Author

Conversion of Raw Soap Response Into HTML

Mark Lee
Greenhorn

Joined: Nov 26, 2003
Posts: 3
Hi,
I'm a novice developer, currently trying out the Amazon Webservices (www.amazon.com/webservices). A sample Amazon Client that I have found (http://developers.sun.com/dev/edu/camps/demos/snc_web/download.html), generates a RAW Soap response onto a browser, as shown below.

******************
Request
Content-Type: text/xml; charset="utf-8"
Content-Length: 956
SOAPAction: "http://soap.amazon.com"
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://soap.amazon.com" env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><env:Body><ns0:KeywordSearchRequest><KeywordSearchRequest href="#ID1"/></ns0:KeywordSearchRequest><ns0:KeywordRequest id="ID1" xsi:type="ns0:KeywordRequest"><keyword xsi:type="xsd:string">cats</keyword><page xsi:type="xsd:string">1</page><mode xsi:type="xsd:string">books</mode><tag xsi:type="xsd:string">webservices-20</tag><type xsi:type="xsd:string">heavy</type><devtag xsi:type="xsd:string">D11ZG20ADM4S1A</devtag><sort xsi:type="xsd:string" xsi:nil="1"/><variations xsi:type="xsd:string" xsi:nil="1"/><locale xsi:type="xsd:string" xsi:nil="1"/></ns0:KeywordRequest></env:Body></env:Envelope>
Response
Content-Type: text/xml; charset="utf-8"
Content-Length: 956
SOAPAction: "http://soap.amazon.com"

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:amazon="http://soap.amazon.com"><SOAP-ENV:Body><namesp721:KeywordSearchRequestResponse xmlns:namesp721="http://soap.amazon.com"><return xsi:type="amazon roductInfo"><TotalResults xsi:type="xsd:string">11518</TotalResults><TotalPages xsi:type="xsd:string">1152</TotalPages><Details SOAP-ENC:arrayType="amazon etails[10]" xsi:type="SOAP-ENC:Array"><Details xsi:type="amazon etails"><Url xsi:type="xsd:string">http://www.amazon.com/exec/obidos/ASIN/039480001X/webservices-20?dev-t=D11ZG20ADM4S1A%26camp=2025%26link_code=sp1</Url><Asin xsi:type="xsd:string">039480001X</Asin><ProductName xsi:type="xsd:string">The Cat in the Hat</ProductName><Catalog xsi:type="xsd:string">Book</Catalog><Authors SOAP-ENC:arrayType
.......
Product xsi:type="xsd:string">0836217357</Product></SimilarProducts></Details></Details></return></namesp721:KeywordSearchRequestResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>******************

The JSP code (KeywordSearchRequest_SOAP.jsp) that generates the above raw SOAP response is as follows:
<html>
<head>
<%@ page contentType="text/html; charset=UTF-8" %>
<title>KeywordSearchRequest</title>
</head>
<center><h2>KeywordSearchRequest</h2></center>
<%
boolean useProxy = false;
if (useProxy) {
System.setProperty("http.proxyHost", "proxy.ntu.edu.sg");
System.setProperty("http.proxyPort", "8080");
}
// Get our port interface
myamazonclient.AmazonClientGenClient.AmazonSearchService service = new myamazonclient.AmazonClientGenClient.AmazonSearchService_Impl();
myamazonclient.AmazonClientGenClient.AmazonSearchPort port = service.getAmazonSearchPort();
// Get the stub and set it to save the HTTP log.
myamazonclient.AmazonClientGenClient.AmazonSearchPort_Stub stub = (myamazonclient.AmazonClientGenClient.AmazonSearchPort_Stub) port;
java.io.ByteArrayOutputStream httpLog = new java.io.ByteArrayOutputStream();
stub._setTransportFactory(new com.sun.xml.rpc.client.http.HttpClientTransportFactory(httpLog));
// Get the end point address and save it for the error page.
String endPointAddress = (String) stub._getProperty(stub.ENDPOINT_ADDRESS_PROPERTY);
request.setAttribute("ENDPOINT_ADDRESS_PROPERTY", endPointAddress);
%>
<%
// Make it so that multibyte characters can be used
request.setCharacterEncoding("UTF-8");
// Get parameters
myamazonclient.AmazonClientGenClient.KeywordRequest p_keywordSearchRequest = new myamazonclient.AmazonClientGenClient.KeywordRequest();
if (!"".equals(request.getParameter("keywordSearchRequest.keyword")))
p_keywordSearchRequest.setKeyword(request.getParameter("keywordSearchRequest.keyword"));
if (!"".equals(request.getParameter("keywordSearchRequest.page")))
p_keywordSearchRequest.setPage(request.getParameter("keywordSearchRequest.page"));
if (!"".equals(request.getParameter("keywordSearchRequest.mode")))
p_keywordSearchRequest.setMode(request.getParameter("keywordSearchRequest.mode"));
if (!"".equals(request.getParameter("keywordSearchRequest.tag")))
p_keywordSearchRequest.setTag(request.getParameter("keywordSearchRequest.tag"));
if (!"".equals(request.getParameter("keywordSearchRequest.type")))
p_keywordSearchRequest.setType(request.getParameter("keywordSearchRequest.type"));
if (!"".equals(request.getParameter("keywordSearchRequest.devtag")))
p_keywordSearchRequest.setDevtag(request.getParameter("keywordSearchRequest.devtag"));
if (!"".equals(request.getParameter("keywordSearchRequest.sort")))
p_keywordSearchRequest.setSort(request.getParameter("keywordSearchRequest.sort"));
if (!"".equals(request.getParameter("keywordSearchRequest.locale")))
p_keywordSearchRequest.setLocale(request.getParameter("keywordSearchRequest.locale"));
if (!"".equals(request.getParameter("keywordSearchRequest.price")))
p_keywordSearchRequest.setPrice(request.getParameter("keywordSearchRequest.price"));
// Call the web service
myamazonclient.AmazonClientGenClient.ProductInfo result = port.keywordSearchRequest(p_keywordSearchRequest);
String httpLogString = httpLog.toString("UTF-8");

%>
<code>
<%
// Display the results
for (int ii = 0; ii < httpLogString.length(); ++ii) {
char c = httpLogString.charAt(ii);
if (c == '&') out.write("&");
else if (c == '<') out.write("<");
else if (c == '>') out.write(">");
else if (c == '\n') out.write("<br/>\n");
else out.write(c);
}
%>
</code>
</html>

I was wondering how to modify the JSP code (esp the JSP code bold,italic), so as to perform a XSLT on the information stored within the String "httpLogString".
Do I need to create an .xml file before performing XSLT with a XSL?
-- if yes, how do I create a .xml file which excludes the headers (Request, Response headers), astericks at beginning and end of the string "httpLogString", and SOAP tags such as xsi:type="xsd:string".
E.g.
<?xml version="1.0" encoding="UTF-8"?>
<ProductInfo>
<Details url="http://www.amazon.com/exec/obidos/ASIN/039480001X">
<Asin>039480001X</Asin>
<ProductName>The Cat in the Hat</ProductName>
<Catalog>Book</Catalog>
<Authors>
<Author>Seuss</Author>
<Author>Theodor Seuss Geisel</Author>
<Author>Dr. Seuss</Author>
</Authors>
<ReleaseDate>June 1957</ReleaseDate>
<Manufacturer>Random House</Manufacturer>
<ListPrice>$8.99</ListPrice>
<OurPrice>$8.99</OurPrice>
<UsedPrice>$6.95</UsedPrice>
<ImageUrlSmall>http://images.amazon.com/images/P/039480001X.01.THUMBZZZ.jpg</ImageUrlSmall>
<ImageUrlMedium>http://images.amazon.com/images/P/039480001X.01.MZZZZZZZ.jpg</ImageUrlMedium>
<ImageUrlLarge>http://images.amazon.com/images/P/039480001X.01.LZZZZZZZ.jpg</ImageUrlLarge>
</Details>
</ProductInfo>
Or is it possible to simply perform a XSLT on the information stored within the String "httpLogString", without any coding. For example, modify XSL?

Sorry for the lengthy message. I was trying to make the point clear.
Thanks.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Yes, you can execute XSL transformations without a single XML/XSL file on disk. You can create an InputSource object from a String by just wrapping a couple of IO decorators from the java.io.* package...


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
By the way, why do you want to parse the "httpLog"? Wouldn't it be enough just to use the result object returned by the generated stub classes?
Mark Lee
Greenhorn

Joined: Nov 26, 2003
Posts: 3
First of all, thanks for the reply & help. Thanks alot.
My initial purpose in wanting to parse the String "httplog" is to want to create a .xml file from it, and subsequently to perform a xslt on the .xml file created (by linking to a xsl file).
I didnt know that the desired information is stored within the result object returned by the generated stub classes. Thus, I have modified part of the JSP code for now to reflect on a browser part of the information, as shown below:
<%
// Dump some data from Amazons database
for(int index = 0; index < result.getDetails().length; index++)
{
out.write("Product name : " + result.getDetails()[index].getProductName() + "<br/>\nOur Price : " + result.getDetails()[index].getOurPrice() + "<br/>\nImage Small : " + result.getDetails()[index].getImageUrlSmall() + "<br/>\n");
}
%>

Nevertheless, I still wish to be able to convert the raw soap response into normal XML format (without SOAP elements) so as to perform XSLT on the xml by using XSL file, as shown below:
Raw SOAP Response

<Details SOAP-ENC:arrayType="amazonetails[10]" xsi:type="SOAP-ENC:Array"><Details xsi:type="amazonetails"><Url xsi:type="xsd:string">http://www.amazon.com/exec/obidos/ASIN/039480001X/webservices-20?dev-t=D11ZG20ADM4S1A%26camp=2025%26link_code=sp1</Url><Asin xsi:type="xsd:string">039480001X</Asin><ProductName xsi:type="xsd:string">The Cat in the Hat</ProductName><Catalog xsi:type="xsd:string">Book</Catalog><Authors SOAP-ENC:arrayType="xsd:string[2]" xsi:type="SOAP-ENC:Array"><Author xsi:type="xsd:string">Dr. Seuss</Author><Author xsi:type="xsd:string">Theodor Seuss Geisel</Author></Authors><ReleaseDate xsi:type="xsd:string">12 March, 1957</ReleaseDate><Manufacturer xsi:type="xsd:string">Random House Books for Young Readers</Manufacturer><ImageUrlSmall xsi:type="xsd:string">http://images.amazon.com/images/P/039480001X.01.THUMBZZZ.jpg</ImageUrlSmall><ImageUrlMedium xsi:type="xsd:string">http://images.amazon.com/images/P/039480001X.01.MZZZZZZZ.jpg</ImageUrlMedium><ImageUrlLarge xsi:type="xsd:string">http://images.amazon.com/images/P/039480001X.01.LZZZZZZZ.jpg</ImageUrlLarge>

Required XML Format

<?xml version="1.0" encoding="UTF-8"?>
<Details url="http://www.amazon.com/exec/obidos/ASIN/039480001X">
<Asin>039480001X</Asin>
<ProductName>The Cat in the Hat</ProductName>
<Catalog>Book</Catalog>
<Authors>
<Author>Seuss</Author>
<Author>Theodor Seuss Geisel</Author>
<Author>Dr. Seuss</Author>
</Authors>
<ReleaseDate>June 1957</ReleaseDate>
<Manufacturer>Random House</Manufacturer>
<ListPrice>$8.99</ListPrice>
<OurPrice>$8.99</OurPrice>
<UsedPrice>$6.95</UsedPrice>
<ImageUrlSmall>http://images.amazon.com/images/P/039480001X.01.THUMBZZZ.jpg</ImageUrlSmall>
<ImageUrlMedium>http://images.amazon.com/images/P/039480001X.01.MZZZZZZZ.jpg</ImageUrlMedium>
<ImageUrlLarge>http://images.amazon.com/images/P/039480001X.01.LZZZZZZZ.jpg</ImageUrlLarge>
</Details>


Thus, I was wondering whether could you again help to elaborate on how could I modify the JSP code so as to convert the raw SOAP response into a normal required XML format message (excluding the soap strings such as xsi:type="xsd:string").
At present, I could only manage to copy the entire raw SOAP response (minus the astericks), and save in a .xml file (as shown below).
[b]Part of modified JSP code to copy raw SOAP response into .xml file[b]
// Call the web service
myamazonclient.AmazonClientGenClient.ProductInfo result = port.keywordSearchRequest(p_keywordSearchRequest);
String httpLogString = httpLog.toString("UTF-8");
String a = "<?xml:stylesheet type='text/xsl' href='lite-data-to-html.xsl'?>";
%>
<code>
<%

FileOutputStream output; // declare a file output object
PrintStream p; // declare a print stream object
try
{
// Create a new file output stream
// connected to "myfile.xml"
output = new FileOutputStream("C:\\Apache Group\\Tomcat 4.1\\webapps\\ROOT\\myfile.xml");
// Connect print stream to the output stream
p = new PrintStream( output );


// Display the results

p.print(a);

for (int ii = 18; ii < httpLogString.length()-20; ++ii) {
char c = httpLogString.charAt(ii);
if (c == '"') p.print("'");
else p.print(c);
}

p.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}
response.sendRedirect("http://localhost:8080/myfile.xml");
%>
</code>

Again, thanks for the help. Thanks.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
How about writing a method such as
Mark Lee
Greenhorn

Joined: Nov 26, 2003
Posts: 3
Hi,
Thanks for the reply. But after thinking through the sample code you have provided, I stil couldn't think of the way to parse the string "httplog" to form a ".xml" file of normal format (without soap entities i.e. excluding the soap strings such as xsi:type="xsd:string").
I guess in the meantime I will try to concentrate on creating a GUI based on the object "result". Nevertheless, thanks alot for the help.
Btw, wish to point out one thing for the Discussion Forum in general. Is it possible to change the way on how words can be formatted e.g. the way to BOLD words. I was thinking the discussion forum would be more user-friendly if user is allowed to highlight the words (that are required to be BOLD etc.) then click the BOLD button. Instead of clicking the BOLD button before entering the words.
Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Conversion of Raw Soap Response Into HTML
 
Similar Threads
Soap Envelope not version 1.1
what's wrong with this SOAP1.2 MESSAGE?
customizing soap messages
problem with perl web services and java client
NullPointerException returned from webservice