如何使用XOM流式传输XML数据? [英] How to stream XML data using XOM?

查看:177
本文介绍了如何使用XOM流式传输XML数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想使用 XOM 将一大堆搜索结果(如XML)输出到PrintWriter或OutputStream中一>。生成的XML如下所示:

Say I want to output a huge set of search results, as XML, into a PrintWriter or an OutputStream, using XOM. The resulting XML would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<resultset>
    <result>
       [child elements and data]
    </result>
    ...
    ...
    [1000s of result elements more]
</resultset>

因为生成的XML文档可能很大(可能是几百兆字节),我想输出它以流式方式(而不是在内存中创建整个文档,然后写入)。

Because the resulting XML document could be big (hundreds of megabytes, perhaps), I want to output it in a streaming fashion (instead of creating the whole Document in memory and then writing that).

输出一个< result> <的粒度/ code>一次很好,所以我想在另一个之后生成一个< result> ,并将其写入流中。换句话说,我只想做类似这样的伪代码(启用自动刷新,所以不要担心):

The granularity of outputting one <result> at a time is fine, so I want to generate one <result> after another, and write it into the stream. In other words, I'd simply like to do something like this pseudocode (automatic flushing enabled, so don't worry about that):

open stream/writer
write declaration
write start tag for <resultset>
while more results:
    write next <result> element
write end tag for <resultset> 
close stream/writer

我一直在看 序列化程序 ,但必要的方法, writeStartTag(Element) writeEndTag(Element) write(DocType)受到保护,而不是公开!除了将Serializer子类化为能够使用这些方法,或者将开始和结束标记作为字符串直接手动写入流中,完全绕过XOM之外,没有其他方法吗? (后者在这个简单的例子中不会太糟糕,但在一般情况下会变得非常难看。)

I've been looking at Serializer, but the necessary methods, writeStartTag(Element), writeEndTag(Element), write(DocType) are protected, not public! Is there no other way than to subclass Serializer to be able to use those methods, or to manually write the start and end tags directly into the stream as Strings, bypassing XOM altogether? (The latter wouldn't be too bad in this simple example, but in the general case it would get quite ugly.)

我错过了什么或是XOM是不是为此而做的?

使用 dom4j 我可以使用轻松完成此操作 XMLWriter - 它的构造函数采用 Writer OutputStream 和方法 writeOpen(Element) writeClose(Element) writeDocType (DocumentType)等。与XOM的 Serializer 比较,其中唯一的公共方法是一个需要整个文档

With dom4j I could do this easily using XMLWriter - it has constructors that take a Writer or OutputStream, and methods writeOpen(Element), writeClose(Element), writeDocType(DocumentType) etc. Compare to XOM's Serializer where the only public write method is the one that takes a whole Document.

(这与关于最好的dom4j的问题替换,其中XOM是一个强有力的竞争者。)

(This is related to my question about the best dom4j replacement where XOM is a strong contender.)

推荐答案

我遇到了同样的问题,但发现它很漂亮简单地做你提到的选项和子类Serializer,如下所示:

I ran in to the same issue, but found it's pretty simple to do what you mentioned as an option and subclass Serializer as follows:

public class StreamSerializer extends Serializer {

    public StreamSerializer(OutputStream out) {
        super(out);
    }

    @Override
    public void write(Element element) throws IOException {
        super.write(element);
    }

    @Override
    public void writeXMLDeclaration() throws IOException {
        super.writeXMLDeclaration();
    }

    @Override
    public void writeEndTag(Element element) throws IOException {
        super.writeEndTag(element);
    }

    @Override
    public void writeStartTag(Element element) throws IOException {
        super.writeStartTag(element);
    }

}

然后你仍然可以利用各种XOM配置,如setIdent等,但使用它如下:

Then you can still take advantage of the various XOM config like setIdent, etc. but use it like this:

Element rootElement = new Element("resultset");
StreamSerializer serializer = new StreamSerializer(out);
serializer.setIndent(4);
serializer.writeXMLDeclaration();
serializer.writeStartTag(rootElement);
while(hasNextElement()) {
    serializer.write(nextElement());
}
serializer.writeEndTag(rootElement);
serializer.flush();

这篇关于如何使用XOM流式传输XML数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆