使用StaxEventItemWriter构建非平凡的XML文件

使用StaxEventItemWriter构建非平凡的XML文件,第1张

使用StaxEventItemWriter构建非平凡的XML文件

摘自spring.io [论坛]
默认的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("\{(.*)\}.*", ""); groupTagName = groupTagName.replaceAll("\{.*\}(.*)", ""); if (groupTagName.contains(":")) {     groupTagNameSpacePrefix = groupTagName.replaceAll("(.*):.*", "");     groupTagName = groupTagName.replaceAll(".*:(.*)", ""); }        }        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("\{.*\}(.*)", ""); if (groupTagName.contains(":")) {     groupTagNameSpacePrefix = groupTagName.replaceAll("(.*):.*", "") + ":";     groupTagName = groupTagName.replaceAll(".*:(.*)", ""); }        }        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;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5507333.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存