简体   繁体   English

如何从一个XML中提取字节数组并将其存储在Java中的另一个XML中

[英]How to extract byte-array from one xml and store it in another in Java

So I am using DocumentBuilderFactory and DocumentBuilder to parse an xml. 所以我正在使用DocumentBuilderFactory和DocumentBuilder来解析xml。 So it is DOM parser. 因此是DOM解析器。

But what I am trying to do is extract byte-array data (its an image encoded in base64) 但是我想做的是提取字节数组数据(其图像以base64编码)

Store it in one object and later in code write it out to another xml encoded in base64. 将其存储在一个对象中,然后在代码中将其写到以base64编码的另一个xml中。

What is the best way to store this in btw. 将其存储在btw中的最佳方法是什么。 Store it as string? 将其存储为字符串? or as ByteArray? 还是作为ByteArray?

How can I extract byte array data in best way and write it out. 如何以最佳方式提取字节数组数据并将其写出。

I am not experienced with this so wanted to get opinion from the group. 我对此没有经验,所以想征询小组的意见。

UPDATE: I am given XML I do not have control of incoming XML that comes in binary64 encoded 更新:我得到的XML我不能控制以binary64编码的传入XML

< byte-array >
  ... base64 encoded image ...
< /byte-array >

Using parser I have I need to store this node and question is should that be byte or string and then writing it out to another node in new xml. 使用解析器,我需要存储此节点,问题是该字节或字符串,然后将其写到新xml中的另一个节点中。 again in base64 encoding. 再次使用base64编码。

thanks 谢谢

The image should be stored in the first xml as a string. 该图像应作为字符串存储在第一个xml中。 Perhaps something like this: 也许是这样的:

<img src="data:image/gif;base64,sssssssssssss"/>

If you need to write the same data to the second xml just use the same string which is already encoded. 如果需要将相同的数据写入第二个xml,只需使用已编码的相同字符串即可。 If you need to change the image. 如果需要更改图像。 Get the attribute ( element.getAttribute("src") ), decode it with one of the many libraries ( apache commons codec ) and then reencode it as a string for the second xml. 获取属性( element.getAttribute("src") ),使用众多库之一( apache commons编解码器 )对其进行解码 ,然后将其重新编码为第二个xml的字符串。

UPDATE RESPONSE: 更新响应:

As to your update. 至于您的更新。 Inside the <byte-array> element you should have plain text. <byte-array>元素内,您应该具有纯文本。 It could be stored as text and then used as text in the second xml. 它可以存储为文本,然后在第二个xml中用作文本。

Base64 encoding is generally used when you need transport the data over text based protocols like http. 当需要通过基于文本的协议(例如http)传输数据时,通常使用Base64编码。 What Base64 encoding does is encodes the binary data into set of characters which can be sent over text based protocol without any encoding/decoding problems. Base64编码的作用是将二进制数据编码为字符集,这些字符集可以通过基于文本的协议发送,而不会出现任何编码/解码问题。

Not sure if you are sending the xml over the wire, but you can use any of the folloiwng methods 不确定是否通过网络发送xml,但是可以使用任何以下方法

  1. Send the base64 string as simple string. 将base64字符串作为简单字符串发送。 But in this case the onus of encoding and decoding will on sending and receiving application programs. 但是在这种情况下,编码和解码将由发送和接收应用程序来承担。

  2. Use standard base64Binary xml type . 使用标准的base64Binary xml类型 In this case, the parser will take care of decoding the string. 在这种情况下,解析器将负责解码字符串。

There's a class in Apache Commons that will help you ensure data integrity with Base64: Apache Commons中有一个类,可以帮助您确保使用Base64的数据完整性:

import org.apache.commons.codec.binary.Base64;

String yourString = "testing";
byte[] encoded = Base64.encodeBase64(yourString.getBytes());

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html

Although, Base64 data is simply alpha characters in addition to + and / , so there shouldn't be any data loss if you store it in a String. 尽管Base64数据除了+/之外,还只是字母字符,因此,如果将其存储在String中,则不会丢失任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM