This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a question, because I want to check my understanding:
Assumption: All data is shared between all users.
Imagine you want to create bean objects from an XML file in one pass, save them in a collection and later never touch the XML file again. In this scenario a DOM parser wouldn't make sense, because the DOM tree replaces the beans. I could directly read the
data from the DOM model and display it without intermediate steps in the JSP.
When using a data base there is no data model, so the requested data has to be stored in a bean and then read from the bean to display it.
No, I don't think that's right at all. But I also don't think it's wrong, either. To me it's just confusing.
First of all there's that statement about all data being shared among all users. Later you mention JSP which suggests that your question is in the context of web applications, where that statement is certainly not true. Unless you want the data to be shared among all users, in which case you could certainly arrange for that to happen. But it's certainly possible to put data in the request context or the session context, which would limit it to a single user.
And I'm not clear about your XML question either. There are many different ways to extract data from an XML document. Some of them marshal the document directly to Java objects, although those tend not to be Java beans. It would certainly be possible to use a DOM parser and then produce Java beans from the output of that parser, the DOM tree. And I'm not sure what you're asking about the JSP, either. You're better off getting the data into Java beans first and then just accessing them directly through JSTL, but there are JSTL tags which allow you to do e.g. an XML transformation on the DOM tree or you could (shudder...) put Java scriptlets into your JSP to do all kinds of things. If you're looking for a specific rule about what must happen in this case, there isn't one.
Joined: May 14, 2012
Thank you. I'm just asking myself why a DOM tree has to be kept in memory, even though I only access the bean objects that had been generated from the elements.
Basically, streaming- and DOM-parsers work both, but which parser is better, if you just want to read and store data? I read that the DOM model is only useful if the data is manipulated, but in this context it's only read. Against this background, is an unused DOM tree in memory some kind of bad programming style?
I don't think it makes the least bit of difference. Write the code which makes it easiest to extract the data you want. And there's no requirement to keep the DOM tree in memory after you've finished extracting data from it, so don't do that. Basically keeping large amounts of unnecessary data in memory is always bad style.