开发者

Best way to read and render presentation using XML file

Here is my scenario:

  1. I have an XML file where various att开发者_Go百科ributes (TextBox, CheckBox etc) of an HTML form are specified. (this is because, if the form changes later, the user only edits the XML file)

  2. Now, I need to read the XML file and render the presentation accordingly.

So, should I:

  1. Read the XML file everytime I need to render the presentation, OR

  2. Read the file once during initialization of the app and save it some where in the memory? If yes, then how?

File is very small: 2.5KB, 60lines


It probably makes little difference to your code whether you read the XML file once or multiple times. (It affects performance, but that's not the real issue. And you should be able to switch between the two approaches with little effort.)

The real issue is how your rendering code accesses the information (in a sense, metadata) represented by the XML. You have a number of alternatives. For example:

  1. Your rendering code could use the W3C DOM APIs directly (or jdom, dom4j, etc) to access access the metadata ... as it goes.
  2. You could write some extra code to turn the DOM into an in-memory data structure with nicer interfaces.
  3. You could to the same, but using a SAX parser to build the data structure.
  4. You could use some technology like EMF to generate custom Java classes from your XSD, and then load them.

Which is best rather depends on the complexity of your XML (or more precisely, its schema), and how much of your rendering code needs to "touch" the metadata. Generally speaking, the greater the complexity and the more your code depends on it, the more you need to move away from using the DOM directly.


It depends on how big the file is and how often it is read. Mostly, option 2 would be better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜