首页 > 解决方案 > 如何在scala中单独处理大xml有效负载的块

问题描述

我有很大的 xml 有效负载,我有时会得到/我得到它如下(示例)

  <items>
<item>
  <property>1</property>
  <property2>2</property2> 
  <nested-property>
    <property>1</property>
  </nested-property>   
</item>
<item>
  <property>1</property>
  <property2>2</property2> 
  <nested-property>
    <property>1</property>
  </nested-property>   
</item>
<item>
  <property>1</property>
  <property2>2</property2> 
  <nested-property>
    <property>1</property>
  </nested-property>   
</item>
<item>
  <property>1</property>
  <property2>2</property2> 
  <nested-property>
    <property>1</property>
  </nested-property>   
</item>
...
...
...

现在我需要每次选择 n 个元素并将它们发送到下游系统,我如何在 scala 中执行此操作?

标签: xmlscalaxml-parsing

解决方案


找到更简单的方法来做到这一点......

xmlData = XML.loadString(paylaod)
val = xmlData \ "items" \ "item"

chunkSize = 50

xmlData.toList.grouped(chunkSize)
.toList.map(list => "<items>" + list.mkString("") + "</items>")
.toNel

现在我有一个分块 xml 的列表,我以后可以处理每个分块/分组的数据。虽然有点太粗略的解决方案

在此链接上找到了分组解决方案 如何将集合分成批次?


推荐阅读