亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將新元素合并到 xml 文件并按年份分組

將新元素合并到 xml 文件并按年份分組

冉冉說 2022-05-25 16:22:29
我的問題是我想Expenditure通過它們在 XML 文件中存儲我的對象Data,但是對于每個Expenditure實例,都會Year創建新元素,它看起來像這樣:<Expenditures xmlns="testing">  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>  <year xmlns="" y="2014">    <Expenditure>      <date>01-01-2014</date>      <money>EUR 1</money>      <description>bilet autobusowy</description>      <type>TRANSPORT</type>    </Expenditure>  </year>
查看完整描述

1 回答

?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

問題是您將thisYear元素放在循環的每次迭代中 for (Expenditure e : user.getExpenditures())。


我建議以其他方式重寫整個邏輯:在遍歷它們之前按年份對支出進行分組。


Map<Integer, List<Expenditure>> groups = expenditures.stream().collect(Collectors.groupingBy(e -> e.getDate().getYear()));

for (Entry<Integer, List<Expenditure>> entry : groups.entrySet()) {

    thisYear = new Element("year");

    thisYear.setAttribute("y", year + "");

    entry.getValues().forEach( e -> {

        Element expenditure = new Element("Expenditure");

        expenditure.addContent(new Element("date")

                .setText(Utils.getFormattedDateTime("dd-MM-yyyy", new DateTime(e.getDate()))));

        expenditure.addContent(new Element("money").setText(e.getMonetaryAmount().toString()));

        expenditure.addContent(new Element("description").setText(e.getDescription()));

        expenditure.addContent(new Element("type").setText(e.getType().name()));

        thisYear.addContent(expenditure);

    });

    doc.getRootElement().addContent(thisYear);


}

抱歉,如果有任何編譯錯誤,我沒有完整的代碼上下文。


查看完整回答
反對 回復 2022-05-25
  • 1 回答
  • 0 關注
  • 72 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號