0

I am new to Web Service. I want to render Web Service response in my jsp page. Here is response returned by webservice url

URL : https://www.domain.com/webservice/pendingbooks?studentNo=123456 

XML Response:

    <?xml version='1.0' encoding='ISO-8859-1'?>
    <books>
    <booksnotreturned>
        <booksdetails>
            <bookno>123545</bookno>
            <issueddate>16-12-2013</issueddate>
            <duedate>30-01-2014</duedate>
            <amt>1000</amt>
        </booksdetails>
       <booksdetails>
            <bookno>123546</bookno>
            <issueddate>15-12-2013</issueddate>
            <duedate>30-02-2014</duedate>
            <amt>2000</amt>
        </booksdetails>
        <booksdetails>
            <bookno>123547</bookno>
            <issueddate>17-12-2013</issueddate>
            <duedate>25-02-2014</duedate>
            <amt>3000</amt>
        </booksdetails>    
    </booksnotreturned>
    <totaloutstanding>6000</totaloutstanding>
    <bookscount>3</bookscount>
</books>

I want to show result is table format...how to do this?

Update : This what I tried.

 $.ajax({
         type: "get",
         url: serviceURL,
         dataType: "text",
         crossDomain: true,
         success: function (data) {
             alert("success");
             var xmlDoc = $.parseXML(data); // then parse into xml
             var xml = $(xmlDoc);

             var rootTag = xml.find("booksdetails");

             $("#output").append("\
                   <tr class=mainheading>\n\
                     <td colspan=6 class=heading>Book Details</td>\n\
                   </tr>\n\
                   <tr class=heading1>\n\
                    <th>Book Number</th>\n\
                    <th>Issued Date</th>\n\
                    <th>Due Date</th>\n\
                    <th>Amount</th>\n\
                  </tr>");
             rootTag.each(function () {
                   var bookno = $(this).find('bookno').text();
                   var issueddate = $(this).find('issueddate').text();
                   var duedate = $(this).find('duedate').text();
                   var amt = $(this).find('amt').text();                           
                  $("#output").append('<tr><td>' + bookno + '</td><td>' + issueddate + '</td><td>' + duedate + '</td><td>' + amt + '</td></tr>');
           });
         },
         error: function () {
               alert("Error");
        }
      });

1 Answer 1

2

There are a variety of options. I'll assume (since you don't mention) that this call is being made by a web browser, and the browser needs to parse it to display an HTML table.

In this case, I'd use an xslt processor (e.g. http://johannburkard.de/software/xsltjs/) to do the transformation into an HTML fragment. That fragment can be then injected into your HTML DOM for display. (If in fact you are making the call from within the JSP you can use an xsl processor like saxon to do the same thing before it gets to the browser)

An xsl template that might work could be something like this:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="booksnotreturned/booksdetails">
  <tr>
    <td><xsl:select value="bookno/text()"/></td>
    <td><xsl:select value="issuedate/text()"/></td>
    <td><xsl:select value="duedate/text()"/></td>
    <td><xsl:select value="amt/text()"/></td>
  </tr>
</xsl:template>
<xsl:template match="/">
  <table>
    <xsl:apply-templates/>
  </table>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I am calling webservice url from jsp page using $.ajax() call, In success method I tried above code. Its working but have headache as xml result is complex.
Is this right way using api.jquery.com/jQuery.parseXML but what if you don't know xml contents or contents changed ?
Yep, your code is going to cause headaches. Building an XML tree walker is much more complicated than writing a template using XSLT. I recommend you visit the xsltjs website for details on how to use that. Regards to your second question ... there are certain expectations about a web service. If you cannot trust the content from the web service, then you will have to implement logic to test the structure before attempting to parse or display it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.