xml的二进制数据传输和处理

本文介绍如何使用JDOM框架处理XML中的图片文件数据,包括读取文件、Base64编码、写入XML文件及从XML文件中读取并还原图片。
XML中的数据无论采用CASTOR还是JDOM框架都可以进行对图片、文件的传输和处理。在内存中他们以byte[]来表示和存储。下面以文件的方式进行了说明,特别注意的是对于二进制要采用64编码的方式表示在XML文件中。
import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.input.DOMBuilder; import org.jdom.output.XMLOutputter; import org.apache.xerces.impl.dv.util.Base64; import java.io.*; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;
 
public class My64DomTest {
      public static void writeXml() {               String content = "";               try {                       //将文件读入内存字节数组                       InputStream in = new FileInputStream("e:/test.JPG");                       byte[] data = new byte[1024];                       int len = 0;                       ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);                       while ((len = in.read(data)) > 0) {                               baos.write(data, 0, len);                       }                       in.close();                       //对内存数据进行64编码                       BASE64Encoder be = new BASE64Encoder();                       content = be.encode(baos.toByteArray());               } catch (IOException ex) {                       System.out.println("++++++++++++++ Get inputStream error +++++++++++++++++");                       ex.printStackTrace();               }               Element carElement = new Element("car");               Document myDocument = new Document(carElement);               Element make = new Element("make");               make.addContent(content);               carElement.addContent(make);               //将编码后的数据生成xml文件输出               try {                       XMLOutputter outputter = new XMLOutputter("  ", true);                       FileOutputStream out = new FileOutputStream("e:/test.xml");                       outputter.output(myDocument, out);               } catch (java.io.IOException e) {                       e.printStackTrace();               }
      }
      public static void readXml() {               try {                       //这里不要使用SAXBuilder,否则将很慢                       //读入xml文件数据                       DOMBuilder builder = new DOMBuilder();                       Document anotherDocument = builder.build(new File("e:/test.xml"));                       Element carElement = anotherDocument.getRootElement();                       Element make = carElement.getChild("make");                       String aa = make.getText();                         //BASE64Decoder bd = new BASE64Decoder();                       //将图片数据转码后写入文件                       try {                               byte[] bbs = Base64.decode(aa);                               FileOutputStream out = new FileOutputStream("e:/test1.jpg");                               out.write(bbs);                               out.close();                       } catch (IOException e) {                       }               } catch (JDOMException e) {                       e.printStackTrace();               } catch (NullPointerException e) {                       e.printStackTrace();               }
      }
}
 
特别注意注释地方的处理。验证通过完全可以使用。
关于xml的传输和处理的问题虽然有了一定的处理经验但还是应该抽时间在深入的研究一下!
评论 (1) | 阅读 (74) | 收藏 (0) | 分享 | 举报
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值