Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Generating bean objects from XML

    To say it briefly, my goal is to load XML data upon request into a JSP.

    I thought it might work as follows, but I am not sure if my approach is good:
    When a button in the jsp 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? Which is the best way to send a variable from the jsp to the servlet?
    Last edited by noodle_variation_187-X; 05-15-2012 at 10:49 PM.

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

    Default Re: Generating bean objects from XML

    For the client side think in terms of HTML, for starters.
    So you'd simply have a field (hidden or not, I have no idea of the underlying requirements here) in a form and submit that form. That would be the easiest.
    If there's more complications then you might need to build the request in Javascript, but that's entirely down to your requirements.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Re: Generating bean objects from XML

    Thank you. The requirements are quite easy: I just need a dynamic website, that displays data from a static xml-file instead of data base data. After the xml has been parsed, the data sets should be saved in bean objects. For this reason the parsing should better be done on the server side, because it wouldn't make a lot of sense to parse the xml in the browser and then transfer the data back to the server. In this case, it could be easier to use xml combined with an xsl-stylesheet and brew a html-file out of it.

    The requesting jsp contains just some buttons or links that call the associated data sets by their IDs. Hence, I must call a servlet function like selectDataSet(String ID) when I click on a link. This function selects the correct bean object and later displays the bean data in the jsp. Am I on the right track so far or are there better options?

    I am also asking myself if it would be better to parse all xml when the application starts, because it could be inefficient to parse static data upon request. In this way parsing has to be done only once.

    Normally, I would have a Controller Servlet with core functionality like this:
    Java Code:
    protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		String address;
    		if (request.getParameter("btn1") != null) {
    			address = "page1.jsp";
    		} else if (request.getParameter("btn2") != null) {
    			address = "page2.jsp";
    		} else {
    			address = "page3.jsp";
    		}
    In my case, I don't want to call a new JSP, instead I want to update data. Could this be done by just calling a static method from a Java class, which is responsible for XML processing:
    Java Code:
    protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		if (request.getParameter("btn1") != null) {
    			XMLParser.selectData(1);
    		} else if (request.getParameter("btn2") != null) {
    			XMLParser.selectData(2);
    		} else {
    			XMLParser.selectData(3);
    		}
    I don't know if this is a good approach.
    Last edited by noodle_variation_187-X; 05-16-2012 at 04:06 PM.

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

    Default Re: Generating bean objects from XML

    You'll need to bear in mind possible thread clashes, but if the data (and that includes how the static class uses that data) is constant then it's a possible solution.
    I'm not sure on any of that, though.
    You'll need to check on thread safety for things like the DOM (assuming that's what's underlying this).
    If the DOM is OK, then transforming based on the Document and an xsl would be a fairly neat solution.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Re: Generating bean objects from XML

    Is it better to parse the XML on the client or the server to generate beans? In my opinion it would be easier to parse the XML on the server, because in this case I am in Java world, otherwise I have to make use of JavaScript.

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

    Default Re: Generating bean objects from XML

    Well, the client won't generate any beans at all will it?
    It's a browser.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Re: Generating bean objects from XML

    I think the same. In my case the beans are meant to keep data that comes from a static XML (it doesn't change during runtime). Against this background, would it make sense to parse the XML upon application start? Otherwise I had to parse the same data upon every request, which would be a waste of ressources.

    Is there any possibility to use some kind of listener, that calls a method when the XML has changed? If so, would this be the best option for data that doesn't change very often (maybe only once per day)?

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

    Default Re: Generating bean objects from XML

    You could do that.
    Define an interface for accessing the data (this will aloow you to change the implementation should you come up with a better idea).
    Create a concrete class that builds (and holds) whatever model you need from the XML.
    When the class is accessed check the file timestamp (say) and if it's newer than the last read in time then refresh the model.

    You'll need to do some sizing to see how much space this will take up, but that's info you have and not us.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 07:55 AM
  2. Replies: 0
    Last Post: 08-13-2010, 11:20 PM
  3. problems generating random objects
    By mandangalo in forum New To Java
    Replies: 8
    Last Post: 07-30-2010, 04:42 AM
  4. UREGENT-accessing session bean ok but entity bean
    By parimal in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-28-2008, 01:34 PM
  5. Replies: 0
    Last Post: 09-26-2007, 08:36 PM

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
  •