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();
}
}
}
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();
}
}
}
相关文章推荐
- Java实现Flash请求的二进制流图片保存并返回XML信息包含图片访问地址
- Java 根据url下载图片 并 保存到对应的本地的新建文件夹中
- Java 使用urlconnection下载文件或图片并保存到本地
- java中将图片保存到数据库中
- java提高篇(41)--urlconnection下载文件或图片并保存到本地
- Html5 canvas生成图片,通过java进行保存
- AndroidStudio 编辑面板中的各种文件快速在Project视图中选中(图片资源,java类,xml等)
- C#将图片转换二进制保存至Xml
- Selenium实现右键保存图片(Java)
- Java从网络上读取图片并保存至本地
- java-后台生成图表、并将图表保存为PNG图片
- JAVA将图片保存成oracle的Blob类型数据
- Java--多线程读取网络图片并保存在本地
- 让input支持 ctrl v上传粘贴图片? 让input支持QQ截图或剪切板中的图像数据(Java实现保存)
- java实现base64字符串转换成图片保存
- 随机保存图片---记录你的状态----java robot test
- JAVA实现自动打开URL对应的网页并保存为图片-不借助第三方API
- java web 上传保存xml文件
- Java http请求保存图片至本地
- Java如何保存网络上的图片