By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,345 Members | 2,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,345 IT Pros & Developers. It's quick & easy.

How i can populate all fileds dynamically in jsp page based on contents found in xml

P: 1
How i can populate all fileds dynamically in jsp page based on contents
found in xml file?I have written jsp servlets and java class file.
i transferred automatic data from jsp to servlet then to java class
which creates a xml file based on values entered in dynamic jsp page.
Now i want to read all those values entered to xml in my other jsp
page.But as dynamic values can be any in no i don't know how could i
populate all those in my jsp again which sholud read all data from xml.
Items can be any so like we create them dynamically with fileds
item title/link/description.I need to read them from xml file and display them dynamically here.

Expand|Select|Wrap|Line Numbers
  1. <%@ page  language="java"
  2. import="java.io.*"
  3. import="java.util.*"
  4. import="org.jdom.*,
  5.         org.jdom.input.*,
  6.         org.jdom.output.*"
  7. %>
  8. <%
  9.      String s1=null,s2=null,s3=null,s4=null,s5=null,s6=null;
  10. %>
  11. <%
  12.  try{SAXBuilder builder = new SAXBuilder();
  13.       Document doc = builder.build(new File("c:\\Rss.xml"));
  14.       Element root = doc.getRootElement();
  15.       doc.getParent();
  16.       Element channel = root.getChild("channel");//channel
  17.  
  18.       s1=channel.getChild("title").getText();
  19.       s2=channel.getChild("link").getText();
  20.       s3=channel.getChild("description").getText();
  21.  
  22.       List item = channel.getChildren("item");//item
  23.       Iterator itr = item.iterator();
  24.        while (itr.hasNext()) {
  25.         Element items = (Element) itr.next();
  26.         s4=items.getChild("title").getText();
  27.         s5=items.getChild("link").getText();
  28.         s6=items.getChild("description").getText();
  29.              }
  30.  }catch (Exception e) {
  31.       e.printStackTrace();
  32.     }
  33. %>
Expand|Select|Wrap|Line Numbers
  1. <head><title>RSS Channel Display Page</title></head>
  2. <script type="text/javascript">
  3. var browser_name=navigator.userAgent;
  4. function getDeleteButtonForGivenID(idname)
  5. {
  6.     var result="";
  7.     result+="<span style='font-size:16px'>";
  8.     result+="<table cellspacing='0' cellpadding='0' border='0'>";
  9.     result+="\n        <tr><td class='button_side'>";
  10.     result+="\n                <img
  11. src='/castylesr2/images/pagebutton-start.png' width='0' height='0'
  12. id='button_"+idname+"startimg' alt='' />";
  13.     result+="\n            </td>";
  14.     result+="\n            <td class='button_center_new'
  15. id='button_"+idname+"buttoncenter' >";
  16.     result+="\n                <button type='button' value='delete'
  17. onclick='callDeleteThis('task_table_"+idname+"');'
  18. id='button_"+idname+"' title='delete this task'
  19. onmouseover='document.getElementById('button_"+idname+"').className='buttons_onmouseover';document.getElementById('button_"+idname+"buttoncenter').className='button_center_onmouseover';document.getElementById('button_"+idname+"startimg').src='/castylesr2/images/pagebutton-start-over.png';document.getElementById('button_"+idname+"endimg').src='/castylesr2/images/pagebutton-end-over.png';'
  20. onmouseout='document.getElementById('button_"+idname+"').className='buttons';document.getElementById('button_"+idname+"buttoncenter').className='button_center_new';document.getElementById('button_"+idname+"startimg').src='/castylesr2/images/pagebutton-start-over.png';document.getElementById('button_"+idname+"endimg').src='/castylesr2/images/pagebutton-end-over.png';'>";
  21.     result+=                    "delete";
  22.     result+=                "</button>";
  23.     result+="\n            </td>";
  24.     result+="\n            <td class='button_side'>";
  25.     result+="\n                <img
  26. src='/castylesr2/images/pagebutton-end.png' width='0' height='0'
  27. id='button_"+idname+"endimg' alt='' />";
  28.     result+="\n            </td>";
  29.     result+="\n        </tr>";
  30.     result+="\n</table>";
  31.     result+="\n</span>";
  32.     return result;
  33. }
  34. function getElementBasedOnOS(idname)
  35. {
  36.     if(browser_name.indexOf("Mozilla") != -1)
  37.         return document.getElementById(idname);
  38.     else
  39.         return document.all[idname];
  40. }
  41. function callDeleteThis(trname)
  42. {
  43.     var tmp=document.getElementById(trname);
  44.     tmp.innerHTML="";
  45.  
  46. }
  47.  
  48. function getElementBasedOnOS(idname)
  49. {
  50.     if(browser_name.indexOf("Mozilla") != -1)
  51.         return document.getElementById(idname);
  52.     else
  53.         return document.all[idname];
  54. }
  55. function callAddMoreFunction(itemTitle, itemLink, itemDesc)
  56. {
  57.     tasks_counter++;
  58.     var obj = getElementBasedOnOS("tasks_table");
  59.     var counter=1;
  60.     var obj_id=null;
  61.     var id_name="task_table_"+counter;
  62.  
  63.     var main_obj=obj;
  64.     for (;obj_id=getElementBasedOnOS(id_name);)
  65.     {
  66.         if(obj_id==null)
  67.             break;
  68.         var temp_Compname_id=getElementBasedOnOS("itemTitle_"+counter);
  69.         if(temp_Compname_id==null)
  70.         {
  71.             main_obj=obj_id;
  72.             break;
  73.         }
  74.  
  75.         counter++;
  76.         id_name="task_table_"+counter;
  77.         obj_id=null;
  78.     }
  79.  
  80.     var string_tobe_printed="<table><tr><td
  81. id='task_table_"+counter+"'><table><tr><td
  82. style='font-size:12px;font-weight:bold;'> Serial No "+counter+"
  83. </td><td>"+getDeleteButtonForGivenID(counter)+"</td></tr>"+
  84.     "<tr><td width='30%'> itemTitle </td><td><input type='text'
  85. id='itemTitle_" +counter+ "' name='itemTitle_" +counter+ "'
  86. value='"+itemTitle+"' size='60'/> *</td></tr>"+
  87.     "<tr><td width='30%'> itemLink </td><td><input type='text'
  88. id='itemLink_" +counter+ "' name='itemLink_" +counter+ "'
  89. value='"+itemLink+"' size='60'/> *</td></tr>"+
  90.     "<tr><td width='30%'> itemDesc </td><td><textarea rows=5 cols=50
  91. id='itemDesc_" +counter+ "' name='itemDesc_" +counter+ "'
  92. >"+itemDesc+"</textarea></td></tr>"+
  93.  
  94.     "<tr><td width='30%'><br/></td></tr>" +
  95. "</table></td></tr></table>";
  96.  
  97.     if(main_obj.id == obj.id)
  98.     {
  99.        main_obj.innerHTML = main_obj.innerHTML + string_tobe_printed;
  100.          }
  101.     else
  102.     {
  103.         main_obj.innerHTML= string_tobe_printed;
  104.     }
  105.  
  106.     StoreFormData();
  107.     LoadFormData();
  108. }
  109. var TotalFormData=new Array();
  110. function StoreFormData()
  111. {
  112.     TotalFormData=new Array();
  113.     for(var i=1;i <= tasks_counter;i++)
  114.     {
  115.         var Task=new Array(3);
  116.         Task[0]=Task[1]=Task[2]="";
  117.  
  118. Task[0]=getElementBasedOnOS("itemTitle_"+i)!=null?getElementBasedOnOS("itemTitle_"+i).value:"";
  119. Task[1]=getElementBasedOnOS("itemLink_"+i)!=null?getElementBasedOnOS("itemLink_"+i).value:"";
  120. Task[2]=getElementBasedOnOS("itemDesc_"+i)!=null?getElementBasedOnOS("itemDesc_"+i).value:"";
  121.         TotalFormData[i]=Task;
  122.         Task=null;
  123.     }
  124.  
  125. }
  126.  
  127. function LoadFormData()
  128. {
  129.     for(var i=1;i <= tasks_counter; i++)
  130.     {
  131.       if(getElementBasedOnOS("itemTitle_"+i)!=null)
  132. getElementBasedOnOS("itemTitle_"+i).value=TotalFormData[i][0];
  133.       if(getElementBasedOnOS("itemLink_"+i)!=null)
  134.  
  135. getElementBasedOnOS("itemLink_"+i).value=TotalFormData[i][1];
  136.       if(getElementBasedOnOS("itemDesc_"+i)!=null)
  137.  
  138. getElementBasedOnOS("itemDesc_"+i).value=TotalFormData[i][2];
  139.  
  140.     }
  141.     TotalFormData=null;
  142. }
  143.  
  144. </script>
  145. <!-- End of the Dynamic Table structures -->
  146. </head>
  147. <body>
  148.     <body bgcolor="#ffffff">
  149. <form action="RssActionServlet"
  150. onsubmit="getElementBasedOnOS('total_tasks_no').value=tasks_counter;"
  151. id="tasks_form" method="post">
  152.         <input type="hidden" name="sendto" value="InsertAlternative"/>
  153.         <br>
  154. <table align="center" border="1">
  155. <tr align="left" valign="middle">
  156. <td align="right"><b>
  157. Channel Title:</b></td>
  158. <td align="right">
  159. <input name="channel_title" size="66" type="text" value="<%= s1
  160. %>"></td>
  161. </tr>
  162. <tr align="left" valign="middle">
  163. <td align="right">
  164. <b>Channel Link:</b></td>
  165. <td align="right"><input name="channel_link" size="66" type="text"
  166. value="<%= s2 %>"></td></tr>
  167. <tr align="left" valign="middle"><td align="right">
  168. <b>Channel Desc:</b></td>
  169.  <td align="right"><textarea name="channel_desc" rows="5"
  170. cols="50"/><%= s3 %></textarea></td></tr>
  171. </table>
  172. <table align="center" border="1">
  173. <tr align="left" valign="middle">
  174. <td align="right"><b>Item Title:</b></td>
  175. <td align="right"><input name="item1_title" size="66" type="text"
  176. value="<%= s4%>"></td>
  177. </tr>
  178.  <tr align="left" valign="middle">
  179. <td align="right"><b>Item Link:</b></td> <td align="right">
  180. <input name="item1_link" size="66" type="text" value="<%= s5%>"></td>
  181. </tr>
  182. <tr align="left" valign="middle">
  183. <td align="right"><b>Item Desc:</b></td> <td align="right">
  184. <textarea name="item1_desc" rows="5" cols="50" wrap = "virtual"><%=
  185. s6%></textarea></td>
  186. <br><td>
  187. <span style="font-size:16px"><caui:button type="submit" value="Edit"
  188. title="edit alternative" id="EditAlternative"
  189. onclick="callEditThisItem();"/>
  190. <br>
  191. <span style="font-size:16px"><caui:button type="submit" value="Delete"
  192. title="Delete this alternative" id="DeleteAlternative"
  193. onclick="callDeleteThisItem();"/>
  194. </td>
  195. </tr></table>
  196.          <table>
  197.             <tr>
  198.                 <td>
  199.                     <span style="font-size:16px"><caui:button
  200. value='Add More' title='Add More Alternatives. [Alternatives will be
  201. added in the end]' id='addMoreAlternatives' disabled='false'
  202. onclick="callAddMoreFunction('','','');"/></span>
  203.                 </td>
  204.                 <td>
  205.                     <span style="font-size:16px"><caui:button
  206. type='submit' value='Save' title='save alternatives'
  207. id='SaveAlternatives' disabled='false' /></span>
  208.                 </td>
  209.             </tr>
  210.         </table>
  211.         <br/>
  212.     </form>
  213. </caui:pagesection>
  214. <center><p></p></center></body></html>
  215.  
thanks
Vijendra
Oct 3 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.