2011-12-07 36 views
0

我嘗試使用資源文件創建一個XML數據轉換成JSP:創建XML數據集中到JSP

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> 

有任何使用java從myData.properties動態修復「描述」和「值」的任何方式,無論我有什麼樣的問題?

回答

0

加載屬性文件後,迭代鍵。

對於每個鍵,您都會有問題編號和該問題的屬性。對於每個問題的響應屬性,您將有一個響應號碼和響應值。

創建Question對象的地圖或集合。每個Question對象都有一個description屬性和一組響應字符串。

當您遍歷該問題的description屬性時,將設置每個Questiondescription屬性。對於每個響應值屬性,將屬性值添加到響應集合中。

要讓它回到XML,您可以將問題列表展示給JSP,並遍歷每個問題(並在該循環內部遍歷問題的響應)。或者,您可以通過任何常規Java庫將對象直接封裝到XML中。

你並不需要問題屬性的數量,因爲它們是在屬性名稱本身中枚舉的。

+0

感謝Dave的迴應。但問題是,我不知道如何迭代postreties文件加載後 –

+0

@naj_ib哦。嗯。您是否考慮過查看[API文檔](http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html)?這裏有一個[方法](http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#propertyNames()),看起來可能有所幫助。 –

+0

它已完成,現在我的jsp已準備好由Jaxb轉換器進行加載:嘗試{} { synchronized(_unMarshaller){ jaxbElement =(JAXBElement )_unMarshaller.unmarshal(inStream); }(inStream是我的jsp作爲InputStream),但我得到這個Jaxbexception:(JaxbHelper.java:115)意外元素(uri:「」,local:「script」)。預期的元素是<{}問卷> –