您的位置:首页 > 编程语言 > Java开发

java图片保存在xml里

2012-07-27 11:39 239 查看
java图片保存在xml里

dom4j-1.6.1.jar-306 KB

jaxen-1.1-beta-6.jar-238 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后,xml的content节点的内容被格式化,从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();

  }

 }

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息