java - 使用 StaxEventItemWriter 构建重要的 XML 文件

标签 java xml spring-batch jaxb2

我需要将一些数据库表的内容转储到具有这种结构的 XML 文件

<dump>
  <table name="tableName1">
    <records>
      <record>
        <first column name>value</first column name>
        <second column name>value</second column name>
        <third column name>value</third column name>
      </record>
      <record>...</record>
    </records>
  </table>
  <table name="tableName2">...</table>
</dump>

每个表的实际记录数未知,所以我无法将单个表的所有数据存储在内存中并转储到 XML。
我现在的工作定义为:


<job id="dump-database-job">
  <step id="dumpTables">
  <tasklet>
    <chunk reader="dumpReader" processor="dumpProcessor" writer="dumpWriter" commit-interval="100" />
  </tasklet>
  </step>
</job>

<bean name="dumpProcessor" class="RecordBeanToJaxbElementProcessor" />
<bean name="dumpReader" class="CompositeItemReader">
  <property name="delegates">
  <array>
    <ref bean="TABLE_ONE_Reader" />
    <ref bean="TABLE_TWO_Reader" />
    <ref bean="TABLE_NTH_Reader" />
    <!-- Other delegates omitted,one for table,for brevity... -->
  </array>
  </property>
  <property name="name" value="dumpReader" />
</bean>

<bean name="TABLE_ONE_Reader" class="JdbcCursorItemReader">
  <property name="rowMapper">
    <bean name="rowMapper" class="RecordBeanRowMapper">
      <property name="tableName=" value="TABLE_ONE" />
     </bean>
   </property>
   <!--other mandatory property omitted -->
</bean>

<bean name="dumpWriter" class="StaxEventItemWriter" scope="step">
  <property name="resource" value="file:#{jobParameters['outfile']}" />
  <property name="shouldDeleteIfEmpty" value="true" />
  <property name="marshaller" ref="marshaller" />
  <property name="overwriteOutput" value="true" />
  <property name="rootTagName" value="dump" />
</bean>

<bean name="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="supportJaxbElementClass" value="true" />
  <property name="classesToBeBound">
    <array>
      <value>RecordBean</value>
    </array>
  </property>
</bean>  

public class RecordBeanRowMapper implements RowMapper<RecordBean> {
  final static RowMapper<Map<String, Object>> columnMapRowMapper = new ColumnMapRowMapper();
  private String tableName;

  public void setTableName(String tableName) {
    this.tableName = tableName;
  }

  @Override
  public RecordBean mapRow(ResultSet rs, int rowNum) throws SQLException {
    final RecordBean b = new RecordBean();

    b.setTableName(tableName);
    b.setColumnValues(Maps.transformValues(columnMapRowMapper.mapRow(rs, rowNum), new Function<Object, String>()    {
      @Override
      public String apply(Object input) {
        return (input == null ? "NULL" : input.toString();
      }
  }
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(namespace="")
class RecordBean {
  private String tableName;
  @XmlJavaTypeAdapter
  /* Entries are written using an adapter to write data as
   * <key>value</key>
   * <key>value</key>
   * ...
   */
  public Map<String,String> entries = new HashMap<String,String>();
}

/* Use to build item node name using dynamic tableName as
 * <tableName>
 *   <key>value</key>
 *   <key>value</key>
 * </tableName>
 */
public class RecordBeanToJaxbElementProcessor implements ItemProcessor<RecordBean, JAXBElement<?>> {
  @Override
  public JAXBElement<?> process(RecordBean item) throws Exception {
    return new JAXBElement<RecordBean>(new QName(item.getTableName()), RecordBean.class, item);
  }
}

这项工作不完整,不能满足我的需求,因为输出看起来像

<?xml version="1.0" encoding="UTF-8"?>
<dump>
  <!-- First record of table TABLE_ONE -->
  <TABLE_ONE>
    <code>Code one</code>
    <description>A record</description>
    <agomappedtable>xyz</agomappedtable>
    <enumcode>NULL</enumcode>
    <is_persistent>false</is_persistent>
    <keep_history_data>false</keep_history_data>
  </TABLE_ONE>
  <!-- Other tons from TABLE_ONE -->
  <!-- First record of table TABLE_TWO -->
  <TABLE_TWO>
    <code>Code 2</code>
    <description>Another record</description>
    <his_name>no_name</his_name>
  </TABLE_TWO>
  <!-- Other tons from TABLE_TWO -->
  <!-- More tables... -->
</dump>

我认为我必须丰富 writer 和/或 marshaller 组件才能实现我的目标,但我还没有找到继续进行的好方法:(

我的问题是:
如何构建如开头所述的复杂 XML 结构,并使作业完全可重启且占用内存少?

最佳答案

[取自spring.io forum]
默认的 StaxEventItemWriter 不可能做到这一点。我不得不为我的一个项目做类似的事情,并编写了一个自定义的 GroupingStaxEventItemWriter。请参阅下面的代码。您需要针对您的特定用例修改 openGroup 和 closeGroup 方法。请注意,通过直接写入底层 java.io.Writer 而不是通过 XMLEventWriter 来关闭分组标记。这是可重启性所必需的。

public class GroupingStaxEventItemWriter<T> extends StaxEventItemWriter<T> {

    private static final String GROUP_IDENTIFIER = "CURRENT_GROUP";

    private Classifier<T, String> classifier;

    private String currentGroup;

    private XMLEventWriter eventWriter;

    private Writer writer;

    @Override
    public void write(List<? extends T> items) throws XmlMappingException, Exception {
        Map<String, List<T>> itemsGroup = new LinkedHashMap<String, List<T>>();
        for (T item : items) {
            String group = classifier.classify(item);
            if (!itemsGroup.containsKey(group)) {
                itemsGroup.put(group, new ArrayList<T>());
            }
            itemsGroup.get(group).add(item);
        }
        for (String group : itemsGroup.keySet()) {
            if (group == null || !group.equals(currentGroup)) {
                if (currentGroup != null) {
                    closeGroup(currentGroup);
                }
                currentGroup = group;
                if (currentGroup != null) {
                    openGroup(currentGroup);
                }
            }
            super.write(itemsGroup.get(group));
        }
    }

    protected void openGroup(String group) throws XMLStreamException, FactoryConfigurationError {
        String groupTagName = group;
        String groupTagNameSpacePrefix = "";
        String groupTagNameSpace = null;
        if (groupTagName.contains("{")) {
            groupTagNameSpace = groupTagName.replaceAll("\\{(.*)\\}.*", "$1");
            groupTagName = groupTagName.replaceAll("\\{.*\\}(.*)", "$1");
            if (groupTagName.contains(":")) {
                groupTagNameSpacePrefix = groupTagName.replaceAll("(.*):.*", "$1");
                groupTagName = groupTagName.replaceAll(".*:(.*)", "$1");
            }
        }
        XMLEventFactory xmlEventFactory = createXmlEventFactory();
        eventWriter.add(xmlEventFactory.createStartElement(groupTagNameSpacePrefix, groupTagNameSpace, groupTagName));
    }

    protected void closeGroup(String group)
            throws XMLStreamException, FactoryConfigurationError {
        String groupTagName = group;
        String groupTagNameSpacePrefix = "";
        if (groupTagName.contains("{")) {
            groupTagName = groupTagName.replaceAll("\\{.*\\}(.*)", "$1");
            if (groupTagName.contains(":")) {
                groupTagNameSpacePrefix = groupTagName.replaceAll("(.*):.*", "$1") + ":";
                groupTagName = groupTagName.replaceAll(".*:(.*)", "$1");
            }
        }
        try {
            writer.write("</" + groupTagNameSpacePrefix + groupTagName + ">");
        } catch (IOException ioe) {
            throw new DataAccessResourceFailureException("Unable to close group: [" + group + "]", ioe);
        }
    }

    @Override
    protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer)
            throws XMLStreamException {
        this.writer = writer;
        this.eventWriter = super.createXmlEventWriter(outputFactory, writer);
        return eventWriter;
    }

    @Override
    public void open(ExecutionContext executionContext) {
        if (executionContext.containsKey(getExecutionContextKey(GROUP_IDENTIFIER))) {
            currentGroup = executionContext.getString(getExecutionContextKey(GROUP_IDENTIFIER));
        }
        super.open(executionContext);
    }

    @Override
    public void update(ExecutionContext executionContext) {
        executionContext.putString(getExecutionContextKey(GROUP_IDENTIFIER), currentGroup);
        super.update(executionContext);
    }

    @Override
    public void close() {
        if (currentGroup != null) {
            try {
                closeGroup(currentGroup);
            } catch (XMLStreamException e) {
                throw new ItemStreamException("Failed to write close tag for element: " + currentGroup, e);
            } catch (FactoryConfigurationError e) {
                throw new ItemStreamException("Failed to write close tag for element: " + currentGroup, e);
            }
        }
        super.close();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
        Assert.notNull(classifier, "Missing required property 'classifier'");
    }

    public void setClassifier(Classifier<T, String> classifier) {
        this.classifier = classifier;
    }

}

关于java - 使用 StaxEventItemWriter 构建重要的 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21678901/

相关文章:

java - 在 Spring Rest 中将 String 转换为 xml Response

xml - 可以对动态生成的 xml 进行 xsl 转换吗?

Spring 批 : Assemble a job rather than configuring it (Extensible job configuration)

java - 如何使用 Spring-Batch 批量插入?

java - 使用 boolean 值进行循环

JavaFX : Is this javafx-maven plugin configuration correct?

java - Lambda 类型推断推断出 lambda 未抛出的异常类型

c# - 如何在没有模式的情况下解析 XML?

java - Spring Batch 作业抛出 NoSuchMethodError : BinaryExceptionClassifier. 分类(Throwable): boolean 值

java - 是否可以使用 XStream 将对象字段隐式添加到 XML 中?