从XML元素内容中获取InputStream [英] Obtain InputStream from XML element content

查看:278
本文介绍了从XML元素内容中获取InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的servlet的doPost()接收一个HttpServletRequest,其ServletInputStream向我发送了一大块用XML包装的uuencoded数据。例如,有一个元素:

My servlet's doPost() receives an HttpServletRequest whose ServletInputStream sends me a large chunk of uuencoded data wrapped in XML. E.g., there is an element:

<filedata encoding="base64">largeChunkEncodedHere</filedata>

我需要解码块并将其写入文件。我想从块中获取一个InputStream,使用MimeUtility将其解码为流,并使用该流写入文件---我宁愿不将这个大块读入内存。

I need to decode the chunk and write it to a file. I would like to get an InputStream from the chunk, decode it as a stream using MimeUtility, and use that stream to write the file---I would prefer not to read this large chunk into memory.

XML是平的;也就是说,没有太多的筑巢。我的第一个想法是使用SAX解析器,但我不知道如何切换到流来读取块。

The XML is flat; i.e., there is not much nesting. My first idea is to use a SAX parser but I don't know how to do the hand-off to a stream to read just the chunk.

感谢您的想法。

格伦

编辑1:注意JB Nizet在这篇文章。

Edit 1: Note JB Nizet's pessimistic answer in this post.

编辑2:我已在下面肯定地回答了我自己的问题,并在下面标记了maximdim的答案是正确的,即使它没有完全回答这个问题,它确实指引我去StAX API和Woodstox。

Edit 2: I've answered my own question affirmatively below, and marked maximdim's answer below as correct, even though it doesn't quite answer the question, it did direct me to the StAX API and Woodstox.

推荐答案

您可以使用 SAX过滤器或XPath只获取您感兴趣的元素。一旦获得元素的内容,将其传递给 MimeUtility.decode()并将流写入文件。

You could use SAX filter or XPath to get only element(s) you're interested in. Once you have content of your element, pass it to MimeUtility.decode() and write stream to file.

我建议您使用代码示例更新您的问题,并告诉我们什么不起作用。

I suggest you update your question with code sample and let us know what doesn't work.

更新:

以下是使用StaX2解析器(Woodstox)的示例代码。出于某种原因,JDK中包含的StaX解析器似乎没有类似的getText()方法,至少在快速浏览时。

Here is sample code using StaX2 parser (Woodstox). For some reason StaX parser included in JDK doesn't seems to have comparable getText() method, at least at quick glance.

显然,输入(r)和输出(w)可以是任何读者/作者或流 - 例如在这里使用字符串。

Obviously input (r) and output (w) could be any Reader/Writer or Stream - using String just for example here.

    Reader r = new StringReader("<foo><filedata encoding=\"base64\">largeChunkEncodedHere</filedata></foo>");
    Writer w = new StringWriter();

    XMLInputFactory2 xmlif = (XMLInputFactory2)XMLInputFactory2.newInstance();
    XMLStreamReader2 sr = (XMLStreamReader2)xmlif.createXMLStreamReader(r);

    boolean flag = false;
    while (sr.hasNext()) {
        sr.next();
        if (sr.getEventType() == XMLStreamConstants.START_ELEMENT) {
            if ("filedata".equals(sr.getLocalName())) {
                flag = true;
            }
        }
        else if (sr.getEventType() == XMLStreamConstants.CHARACTERS) {
            if (flag) {
                sr.getText(w, false);
                break;
            }
        }
    }
    System.out.println(w);

这篇关于从XML元素内容中获取InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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