Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Dynamic text from XML-file in a JSP

    Hello people,
    i would like to know how to call one and the same JSP-file with different XML-content depending on which link I click. For example, if I click on link1 the JSP-page should load the content from data1.xml. After parsing the XML, a bean should be generated from the XML. However, I don't know if XML should be parsed on the client or on the server in this context.

    My project is quite simple and only for learning purposes:
    I have a start page with several cooking recipes. If I click on one of those recipes I get a detailed view of the recipe, which includes some text and an image. However, I don't want to create a new jsp-file for every recipe, because all recipes have the same layout. Instead, I want to use one and the same jsp-file for an arbitrary amount of recipes. The content of those recipes should be located in one or several XML-files.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,753
    Thanks
    41
    Thanked 191 Times in 190 Posts
    why on earth are you reading from an xml file? set up your recipes in a database and then onclick()
    Code:
    select * from tbl_Recipes
    where recipe_name = @parameter
    if you really want to go with xml files, it seems as though the Java equiv is resultset... you'll have to look- I assume it is more than do-able...
    I would read through the C# methods to get an ide of how easy it can be...
    dataset.readxml(string)
    datatable.readxml(string)

    Important! those are C# examples- they will not work in Java- they don't exist... the logic is a little more clear I think though- I was having trouble finding a decent Java example for you.
    here is oracle's link to the result set

    Edit: I really think though that it would save you a lot of time/effort to just store in database

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    rat_ape (05-15-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you. I thought that a database could be a bit overdimensioned for smaller data sets and also more difficult to handle, but maybe that's not the case. Hence, I just thought I could parse the XML and store the data upon request in bean objects. After that the data is loaded into the JSP depending on which button has been clicked before.
    However, I don't know yet how to associate a button like <input type="button" value="pizza " name="btn1"/> with the related XML-file. I thought it could work this way:
    When the button is clicked, a variable that indicates which XML-file shall be loaded, is sent to a servlet. Depending on the variable that has been sent to the servlet, the servlet loads and parses the related XML-file and stores the data in a bean object. Finally, the JSP-page loads the bean-data by using the useBean-Tag. Could this approach make sense or is there a better one?
    Last edited by rat_ape; 05-15-2012 at 09:42 PM.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,753
    Thanks
    41
    Thanked 191 Times in 190 Posts
    well kind of my point... you are going to have a harder time converting whatever it is that the user selects to translate it to some xml search... think of the alternative... let's say your buttonclick does something like...
    Code:
    button_click_handler(){
    int i = (int)dropdownlist.selectedindex;
    string cmd = "select * from tbl_recipe where tbl_recipe_PKID = " + i;
    // from there- go on to query the database and return that record
    a heck of a lot less code and effort than parsing through some XML file. XML files are a way to get unrelated languages to talk to each other- they shouldn't be used as datatable structures... it becomes a nightmare real quick when you do that
    ... let's say you get your XML 'template' set up... and let's say you have 3 records
    Code:
    <tbl>
      <IDCol>1</IDCol>
      <txt>Demo</txt>
    </tbl>
    <tbl>
      <IDCol>2</IDCol>
      <txt>Demo2</txt>
    </tbl>
    <tbl>
      <IDCol>3</IDCol>
      <txt>Demo3</txt>
    </tbl>
    now let's say there is an oops...
    Code:
    <tbl>
      <IDCol>1</IDCol>
      <txt>Demo</txt>
    </tbl>
    <tbl>
      <IDCol>2</IDCol>
      <txt>Demo2</txt>
    </tbl>
    <tbl>
      <IDCol>3</IDCol>
      <txt>Demo3</txt>
    </tbl>
    <tbl>
      <IDCol>4<IDCol>  <!-- note missing '/' -->
      <txt>oops</txt>
    </tbl>
    your now 'expected' 4 record return with one table, will return an entire set with the data strewn about randomly. it sux

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

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