簡體   English   中英

Create a multiple child object for XML from CSV file in JAVA - JAXB Marshalling

[英]Create a multiple child object for XML from CSV file in JAVA - JAXB Marshalling

我有一個程序可以將我的CSV文件轉換為XML文件。

基於我的XSD每個UserID可以有多個Detail object

這是我期望的結果

<XML>  
  <Plant>
        <UserID>37686</UserID>
        <Verification>
            <Detail>
                <Number>4851</Number>
                <IPF>2</IPF>
            </Detail>
            <Detail>
                <Number>9978</Number>
                <IPF>2</IPF>
            </Detail>
            <Detail>
                <Number>9994</Number>
                <IPF>2</IPF>
            </Detail>
        </Verification>
        <PointNumber>0</PointNumber>
        <Length>127.9</Length>
   </Plant>
   <Plant>
        <UserID>11111</UserID>
        <Verification>
            <Detail>
                <Number>9994</Number>
                <IPF>2</IPF>
            </Detail>
        </Verification>
        <PointNumber>0</PointNumber>
        <Length>0</Length>
   </Plant>
<XML>

這是我的 CSv 文件

User ID; Number; IPF;PointNumber; Length
37686;4851;2;0;127.9
37686;9978;2;0;0
37686;9994;2;0;0
11111;9994;2;0;0

我想在每一行上循環並控制UserID ,如果它等於前一個,它不應該創建另一個Plant object 並且只需將第二個和第三個值作為Detail Object 插入到前一個UserID

這是我的代碼,但我無法實現。

String[] prevvalue = null;
 while ((line = br.readLine()) != null) {   
                    
                    Start++;
                    String[] currentvalue;
                    String Cur = null;
                     currentvalue =line.split(";");
                    Detail Detail = new Detail();
                    ArrayList<Detail> DetailList = new ArrayList();

                    Plant Plant = new Plant();   
                    if (Start >0 && prevvalue!= null) {
                       Cur = currentvalue[0];
                        
                        if (Cur.equals(prevvalue)) {
                            
                            Detail.setNumber(currentvalue[1]);
                            Detail.setIPF(currentvalue[2]); 
                        }
                       elseif (Cur!= prevvalue) {
                            Plant.setUserID(currentvalue[0]);
                            Plant.setPointNumber(currentvalue[3]);
                            impianto.setLength(currentvalue[4]);
                            
                            Detail.setNumber(currentvalue[1]);
                            Detail.setIPF(currentvalue[2]); 
                        }
                        
                    }
                DetailList.add(Detail);
                Plant.setDetail(Detaillist);
                PlantList.add(Plant) ;
                prevvalue = currentvalue[0];

誰能幫我解決這個問題? 問候罪

我找到了解決方案......我的錯誤在於我的對象的定義,我已經在循環內部定義了它們,但是其中一些必須在內部定義,另一個必須在外部定義。

再次感謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM