im trying to create an xml data into JSP using a resource file:
MyData.properties :
nombreOfQuestions=2
question1.description=what is the color?
question1.responseValue1=Red
question1.responseValue2=yellow
question1.responseValue3=white
question2.description=what is the Weight?
question2.responseValue1=70
question2.responseValue2=75
question2.responseValue3=80
myJsp.jsp :
<?xml version="1.0" encoding="UTF-8"?>
<%@ page import="MessageResourcesHelper"%>
<%
MessageResourcesHelper helper = new MessageResourcesHelper (pageContext, "MyData");
String nombreOfQuestions= helper.getProperty ("nombreOfQuestions",0);
%>
<Question>
<description>what is the color?</description>
<response>
<value>Red</value>
<value>Yellow</value>
<value>White</value>
</response>
</Question>
<Question>
<description>what is the Weight?</description>
<response>
<value>70</value>
<value>75</value>
<value>80</value>
</response>
</Question>
is there any way to use java to recuperate the “description” and the “values” dynamically from myData.properties whatever the nombre of question i have?
After you load the properties file, iterate over the keys.
For each key, you’ll have a question number, and a property of that question. For each question’s response properties you’ll have a response number, and the response value.
Create either a map, or a collection, of
Questionobjects. EachQuestionobject has adescriptionproperty, and a collection of response strings.The
descriptionproperty of eachQuestionis set when you iterate over that question’sdescriptionproperty. For each response value property, add the property value to the collection of responses.To get it back to XML, you can either expose the question list to the JSP and iterate over each question (and inside that loop, iterate over the question’s responses). Or you could marshall the object directly to XML via any of the normal Java libraries for doing so.
You don’t really need the number of questions property since they’re enumerated in the property names themselves.