0

I have decided to create dynamic xml as a response of my rest service.

Xml structure is defined in properties file it may change in future.

What will best approach to achieve this task.

Help me out with suggestions friends.

Thanks in advance

9
  • Is there an option to convert the properties file into an XSD ? If you have your xml template saved in a XSD, that will make life lot simpler. Let us know if that is possible. Commented May 19, 2014 at 5:42
  • Properties file contains the hierarchy of the xml.Let us take the scenario my xml has 5 elements like firstname,lastname,age,empid,salary.So each element has its position in properties file so i have read the postion of the element each time to form a xml.Eg : firstname as users/employee/firstname ,secondname users/employee/lastname like goes on.In future i may change the employee as users/employees/person so without code still i can get the different xml structure if i configured the structure in properties.I hope its clears. Commented May 19, 2014 at 5:47
  • Yes, you can move the same to an xsd. If you have a XML Schema Definition, then creaing the xml will be more standardized and robust. XSD itself being a XML, modifying it will be easier than modifying the properties file. Commented May 19, 2014 at 5:48
  • But enduser can't modify the xsd easily right and to modify xsd technical knowledge is required.If it is property file any one can easily modify it just a key value pair text file . correct me if am wrong. Commented May 19, 2014 at 5:53
  • Maybe, can you provide a sample properties file content? Commented May 19, 2014 at 5:55

3 Answers 3

1

It is not recommended to use the properties file for generating dynamic XML. If the client requirement is that you have to used that properties file. Else the recommended way is to use the XSD schema generation method.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the javax.xml.stream.XMLOutputFactory to generate the XML output. You can read the XML structure from the property file as your requirement and can generate the output using the javax.xml.stream.XMLOutputFactory.

Hope following code will helpful to you.

StringWriter stringWriter = new StringWriter();
XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
XMLStreamWriter writer = xmlFactory.createXMLStreamWriter(stringWriter);

writer.writeStartDocument();
writer.writeStartElement(<<First element>>);

6 Comments

And how do you recommend, reading the properties file and getting the element names/hierarchies?
To avoid code change in future we decide to go with this approach.If it is not good suggest me better way.
firstname.xml_payload_structure = "user_profile/user/firstname lastname.xml_payload_structure = "user_profile/user/lastname age.xml_payload_structure = "user_profile/user/age.. It may contain nested xml tags too.
Sridhar, DOM will be good approach when the XML file is not large. write huge amounts of data it is better to use SAX, SAX also provides higher performance.
Okay thanks.The code you mentioned above is SAX right?
|
0
  1. Using the Properties propertyNames() we will be getting the list of all the keys.
  2. Using keys we can find the values.
  3. Using this we can create a xml file using StringBuilder

For Example

StringBuilder sb = new StringBuilder(); sb.append("<"+key+">"); sb.append("+value+"); sb.append("</"+key+">");

After this write it to a file . Using FileOutputStream.java

4 Comments

You can load the properties file using
Yes but we are defining the structure alone in properties not values .
If the values are not there then its ok to have just the keys with out values
We are getting the structure of the xml from properties file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.