开发者

XML template in Java

开发者 https://www.devze.com 2022-12-11 02:59 出处:网络
I need to generate XML and they differ only in the values, th开发者_高级运维at the tags contain.

I need to generate XML and they differ only in the values, th开发者_高级运维at the tags contain.

Is it possible to create a template XML and then write only the values each time? (I do not want to go the JAXB way as these are small XMLs and are not worth creating objects for them).

Is this a good approach?

Any thoughts?


You can use freemarker or velocity for templating in java -- or even just add PHP tags to a sample XML to generate from a template.

I think as a general rule, though, once you start conditionally adding elements or attributes, or looping to generate multiples, you're better of generating your XML -- though I agree sometimes getting it into a format you want (not what the generator wants) is sometimes a pain.


As long as the XML file to be produced is small, simple and mostly consistent in format, I tend to buck the trend: I simply create and write a text string.

writer.out.format("<?xml version='1.0'><root><tag1>%s</tag1></root>", value1)

kinda thing.


Despite the fact that you are against jaxb (which I have yet to use), I wish to recommend a comparable way to do this with Apache's XMLBeans.

This requires you to use an xml schema - but from my experience it worth it...

0

精彩评论

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