java图片保存在xml里

java图片保存在xml

dom4j-1.6.1.jar306 KB

jaxen-1.1-beta-6.jar238 KB

 

public class ImageToXml {
 private String ix = null;// image or xml file nam
文件路径

 public ImageToXml(String imageOrXml) {
  this.ix = imageOrXml;
 }

 private String readImage() {//读取图片,返回BASE64Encoder类型字符串
  BufferedInputStream bis = null;
  byte[] bytes = null;
  try {
   try {
    bis = new BufferedInputStream(new FileInputStream(ix));
    bytes = new byte[bis.available()];
    bis.read(bytes);
   } finally {
    bis.close();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return new BASE64Encoder().encodeBuffer(bytes);
 }

 public void imageToXml() {
  String xml = "" + "<image>" + "<name>" + ix + "</name>" + "<content>"
    + readImage() + "</content></image>";
  try {
   XMLHelper.write(XMLHelper.parseText(xml), ix + ".xml");
  } catch (DocumentException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public void xmlToImage(String rename) {//xml解析出图片
  Document d;
  String name = null;
  String content = null;
  try {
   d = XMLHelper.parse(ix);
   name = XMLHelper.getNodeValue(d, "/image/name");
   content = XMLHelper.getNodeValue(d, "/image/content");
   saveImage(rename.equals("") ? name : rename, content);
  } catch (DocumentException e) {
   e.printStackTrace();
  }
 }

 public void xmlToImage() {
  xmlToImage("");
 }

 private void saveImage(String filename, String content) {
  try {
   DataOutputStream dos = null;
   try {
    byte[] bs = new BASE64Decoder().decodeBuffer(content);
    dos = new DataOutputStream(new BufferedOutputStream(
      new FileOutputStream(filename)));
    dos.write(bs);
   } finally {
    dos.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  ImageToXml itx = new ImageToXml("F://1.jpg");
  System.out.println(itx.readImage());
  itx.imageToXml();
  ImageToXml itx1 = new ImageToXml("F://1.jpg.xml");
  itx1.xmlToImage("F://2.jpg");
 }
}

 

public class XMLHelper {
 
 public static Document parse(String fileName) throws DocumentException {
  SAXReader reader = new SAXReader();
  Document document = reader.read(fileName);
  return document;
 }

 
 public static Document parseText(String text) throws DocumentException {
  return DocumentHelper.parseText(text);
 }

 
 public static String parseDocument(Document document)
   throws DocumentException {

  return document.asXML();
 }

 
 public static void modifyAttribute(Document document, String xPath,
   String attributeName, String newValue) throws DocumentException {

  XPath xpathSelector = DocumentHelper.createXPath(xPath);

  Element element = (Element) xpathSelector.selectSingleNode(document);

  element.setAttributeValue(attributeName, newValue);
 }

 
 public static void deleteNode(Document document, String xPath) {
  XPath xpathSelector = DocumentHelper.createXPath(xPath);
  List list = xpathSelector.selectNodes(document);
  for (int i = 0; i < list.size(); i++) {
   Node node = (Node) list.get(i);
   node.detach();

  }
 }

 
 public static void modifyNodeValue(Document document, String xPath,
   String newValue) {
  XPath xpathSelector = DocumentHelper.createXPath(xPath);

  Element element = (Element) xpathSelector.selectSingleNode(document);
  if (element.isTextOnly()) {
   element.setText(newValue);
  } else {
   throw new InvalidXPathException(xPath, "
XPath未选取到原子节点!");
  }

 }

 
 public static String getNodeValue(Document document, String xPath) {
  XPath xpathSelector = DocumentHelper.createXPath(xPath);
  Element element = (Element) xpathSelector.selectSingleNode(document);
  String result = "";
  if (element.isTextOnly()) {//
判断是否是原子节点
   result = element.getText();
  } else {
   result = element.asXML();

  }

  return result;

 }

 
 public static void write(Document document, String fileName)
   throws IOException {
  //
这里使用的format后,xmlcontent节点的内容被格式化,从xml读取图片时会出现问题
  //OutputFormat format = OutputFormat.createPrettyPrint();
  XMLWriter writer = new XMLWriter(new FileWriter(fileName));
  writer.write(document);
  writer.close();

 }

 
 public static void writeWithEncoding(Document document, String fileName,
   String encoding) throws IOException {

  OutputFormat format = OutputFormat.createPrettyPrint();
  format.setEncoding(encoding);
  XMLWriter writer = new XMLWriter(new FileWriter(fileName), format);
  writer.write(document);
  writer.close();

 }

 public static void main(String[] args) {

  try {
   String fileName = "F://1.xml";
   Document document = parse(fileName);
   String xPath = "/personnel/person[@id='two.worker']";
   XMLHelper.deleteNode(document, xPath);
   // XMLHelper.modifyNodeValue(document, xPath1, "
淡淡的");
   //
未选取到原子节点,抛出异常。
   System.out.println(XMLHelper.getNodeValue(document,
     "/personnel/person[@id='four.worker']"));
   System.out.println(XMLHelper.getNodeValue(document,
     "/personnel/person[@id='four.worker']/name"));
   System.out.println(XMLHelper.getNodeValue(document,
     "/personnel/person[@id='four.worker']/email"));
   XMLHelper.write(document, fileName);

  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值