【问题标题】:How to write a file to xml with STAX?如何使用 STAX 将文件写入 xml?
【发布时间】:2012-03-18 13:33:25
【问题描述】:

我在将文件写入 xml 时遇到问题。 这是该元素在 xsd 中的外观。

<xs:element name="File">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:base64Binary">   
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

这是我的写作方法:

       private void writeFile (XMLStreamWriter2 sw, final InputStream is){
//is - inputstream from file
            try {
                OutputStream output = null;
                InputStream input = new Base64InputStream(is, true);
                int count;

                    int BUFFER_SIZE = 4000;
                    byte[] buffer = new byte[BUFFER_SIZE];
                    output = new FileOutputStream(new File("D:\\test.txt"));
                    while ((count = input.read(buffer)) != -1) {

                            output.write(buffer, 0, count);
                            sw.writeBinary(buffer, 0, count);

                    }
                    is.close();
                    input.close();
                    output.close();
            } catch (XMLStreamException ex) {
                Logger.getLogger(StaxFinal.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(StaxFinal.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }

test.txt 的内容是有效的 base-64 字符串,而 File xml 元素文件的内容不是(我正在检查 http://www.opinionatedgeek.com/dotnet/tools/base64decode/)。为什么

编辑 尝试使用它,但我得到很多回车符号#xd;

private void writeFile (XMLStreamWriter sw, InputStream is){
   //is - FileInputStream
    Reader reader = new InputStreamReader(new Base64InputStream(is, true));
    char[] buf = new char[4096];
    int n;

        while( (n = reader.read(buf)) >= 0 ) {
                sw.writeCharacters(buf, 0, n-3 );
        }
}

编辑 方法:

writeRaw(char[] chars, int i, int i1)

工作正常。奇怪,但是woodstox 的readElementAsBinary 为我读取和解码base64。为什么writeBinary 不写有效的base64? 无论如何,谢谢你skaffman!你太棒了!

【问题讨论】:

  • writeBinary 是 Woodstox STAX 实现中的专有方法,因此您不应该真正使用它。坚持标准的XMLStreamWriter接口。
  • 哇,谢谢。但是如何从流中写入?使用writeCharacters(char[] text, int start, int len)???
  • 是的。 XML 只能表示字符数据,所以如果你选择将 base64 编码的二进制数据写成字符数据,那么你应该自己编码。 Woodstox writeBinary 的东西是为了他们的高性能网络服务支持(例如快速信息集),所以除非你真的知道你在做什么,否则你应该避免它。
  • 请看我的编辑。如何摆脱#xd;符号。

标签: java xml io stax


【解决方案1】:

我假设您使用来自 Apache Commons Codec 的 Base64InputStream。它还提供了一个带有四个参数的构造函数,可用于完全关闭换行符。 根据问题调整您的方法,您可以像这样编写有效的 Base64 输出:

private void writeFile (XMLStreamWriter sw, InputStream is){
    //is - FileInputStream
    Reader reader = new InputStreamReader(new Base64InputStream(is, true, -1, null));
    char[] buf = new char[4096];
    int n;
    while( (n = reader.read(buf)) >= 0 ) {
        sw.writeCharacters(buf, 0, n);
    }
}

【讨论】:

    【解决方案2】:

    基于 Base64InputStream 的 javadoc:如果您向构造函数提供两个参数(InputStream in,boolean doEncode),那么它创建一个 Base64InputStream,以便所有读取的数据都是 Base64 编码或 Base64 解码的原始数据提供了 InputStream。doEncode - 如果我们应该对从我们读取的所有数据进行编码,则为 true,如果我们应该解码,则为 false。

    因为我不确定你的函数应该做什么,我只能侦察这个:

    • 您可以将 doEncode 设置为 false(将解码后的文本存储在您的 xml 文件中)
    • 或直接使用输入流is(将文本文件复制/粘贴到xml)

    【讨论】:

    • 感谢您的回答。我需要将编码文本存储在 xml 中(xsd 要求)。我不明白为什么 test.txt 可以,而 xml 内容不行。
    • 更改 InputStream 输入 = new Base64InputStream(is, true);到 InputStream 输入 = is;
    • 已更改,打开创建的 xml,从 File 元素复制文本,粘贴到我链接的在线解码器,The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2011-08-23
    • 2015-05-10
    • 2011-09-06
    • 2021-12-27
    • 1970-01-01
    • 2011-02-26
    相关资源
    最近更新 更多