Results 1 to 10 of 10
  1. #1
    sandy1000's Avatar
    sandy1000 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Smile read an xml file and display using jsp

    first prepare an xml file as defined below and name it as test.xml
    <?xml version="1.0" encoding="iso-8859-1"?>
    <CATALOG>
    <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
    </CD>
    <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
    </CD>
    </CATALOG>


    Now prepare the jsp file as defined below as get_cd_catalog.jsp

    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    xmlDoc=xmlhttp.responseXML;
    var txt="";
    x=xmlDoc.getElementsByTagName("TITLE");
    y=xmlDoc.getElementsByTagName("ARTIST");
    z=xmlDoc.getElementsByTagName("COUNTRY");

    alert(x);
    for (i=0;i<x.length;i++)
    {
    txt=txt + x[i].childNodes[0].nodeValue +" ";
    txt=txt + y[i].childNodes[0].nodeValue +" ";
    txt=txt + z[i].childNodes[0].nodeValue +"<br />";
    }
    document.getElementById("myDiv").innerHTML=txt;
    }
    }
    xmlhttp.open("GET","test.xml",true);
    xmlhttp.send();
    }
    </script>
    </head>

    <body>

    <h2>My CD Collection:</h2>
    <div id="myDiv"></div>
    <button type="button" onClick="loadXMLDoc()">Get my CD collection</button>

    </body>
    </html>

  2. #2
    gajendra1987 is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default

    Thats a good suggestion given by Sandy

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    What does that have to do with Java?
    There's no Java, or JSP tags, in there at all are there?

  4. #4
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Default Re: read an xml file and display using jsp

    Quote Originally Posted by Tolls View Post
    What does that have to do with Java?
    There's no Java, or JSP tags, in there at all are there?
    the jsp tags are inside <body>

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: read an xml file and display using jsp

    Where?
    Java Code:
    <body>
       <h2>My CD Collection:</h2>
       <div id="myDiv"></div>
       <button type="button" onClick="loadXMLDoc()">Get my CD collection</button>
    </body>
    That's the body of the page.
    There are three tags there...<h2>, <div> and <button>. All of those are html, not jsp.

    And this is three years old....
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: read an xml file and display using jsp

    Even when there are no JSP tags, that doesn't stop you from doing the above in a JSP though :) It makes little sense to do because you might as well just put it in a HTML file of course.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: read an xml file and display using jsp

    Well, yes, but as my post from 2 1/2 years ago says, "what does that have to do with Java?"...
    It's all a bit pointless.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: read an xml file and display using jsp

    Alright, let me shove my opinion out the door here and lets forget that this thread exists for the purpose of it. I disagree, its not pointless. I've seen the "java only" argument on OTN also a lot and in 99% of the cases I agree with it, but in the case of JSPs I do not. Push comes to shove, JSPs go beyond Java and incorporate HTML, Javascript and CSS too. In fact a good JSP contains no Java code at all which turns it into a huge gray area because now you have to bend the "java only" rules to include JSTL, or JSF tags for example. Its not Java code but it is certainly Java related.

    Someone working with JSPs might run into the requirement of needing to do some Ajax stuff, which is 100% javascript but is also 100% relevant to the JSP development process - doesn't that make it Java related in the same way that JSTL and JSF tags are Java related? See what I'm getting at?

    But that's just my opinion of course :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: read an xml file and display using jsp

    No, I agree, in general.
    I let Ajax go by because it is such an important part of webby stuff. You can't ignore the front end, but it needs to have some association with the Java back end.
    As for the JSTL/JSF tags, those are Java...they may be disguised (in the same way a JSP is a servlet in disguise), but it's Java behind them. IMO...:)

    In this case it's just an HTML page.
    Worse than that, it's an HTML page geared entirely to Windows.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: read an xml file and display using jsp

    Woohoo, we agree! And yes those last points you make is why I stated to ignore this thread for the sake of the discussion ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  2. how to read an image and display
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2009, 12:43 PM
  3. Replies: 3
    Last Post: 01-29-2009, 10:20 AM
  4. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 11:45 AM
  5. Simplest way to read and display a jpeg image
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 03:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •