0

My requirement is that I have a number of XML templates and depending on certain decision params, I have to populate it either with values given by user or from a property file. The XMLs are quite long and nested. I understand any of the following approaches need to be taken for the solution:

1). First unmarshall the xml into a java object >> populate the object from user/property file >> again marshall the resulting object to get the final xml. 2). search for the pattern of tags and replace it with the given values.

I believe former approach is better since it Object oriented and less errorneous, though it will be a big task to populate each and every instance variable of the object.

Also that the latter may result in a cumbersome code as well as lots of errors as pattern may be different : for e.g.,

 > <maven></maven>

 or <maven />.

My question is, is there any other promising and faster approach that can be taken to achieve this?

Any help will be highly appreciated.

sonal
  • 101
  • 1
  • 4
  • 13
  • a quick google will give you several libraries that can parse the xml for you, you can even generate the java classes from xml online, example: http://pojo.sodhanalibrary.com/ (google first result for 'xml to java online') – Mário Fernandes Sep 05 '17 at 06:29
  • That is again the first approach ...right? – sonal Sep 05 '17 at 06:48
  • yes, I would go for that one, normally it should make the code more readable and easier to maintain. You can change the tags values as well, but out of the box I find it more complicated (even though it might be less code) – Mário Fernandes Sep 05 '17 at 07:04

1 Answers1

0

You might use xpath for extracting xml tags.

How to read XML using XPath in Java

Also it should be tried to see if it is faster.

yılmaz
  • 1,818
  • 13
  • 15