xml儲存圖片
package com.kelsen.beans.imagehelper; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class ImageToXML { /** * 把圖片轉成 BASE64Encoder * @param str_FileName * @return */ public static String readImage(String str_FileName) { BufferedInputStream bis = null; byte[] bytes = null; try { try { bis = new BufferedInputStream(new FileInputStream(str_FileName)); 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); } /** * 把BASE64Decoder轉成圖片 * @param filename * @param content */ public static 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(); } } }
解析xml
package com.kelsen.beans.xmlhelper; import java.io.File; import java.util.Vector; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class KParseXML { /******************************** * 指定檔案名稱,擷取Document對象 * @param str_FileName * @return ********************************/ public static Document createDocument(String str_FileName){ Document document_result=null; try { DocumentBuilderFactory documentBuilderFactory; DocumentBuilder documentBuilder; documentBuilderFactory=DocumentBuilderFactory.newInstance(); documentBuilder=documentBuilderFactory.newDocumentBuilder(); document_result=documentBuilder.parse(str_FileName); } catch (Exception e) { e.printStackTrace(); } return document_result; } ////////////////////////////////////// ////節點、屬性、值 尋找部分 ////////////////////////////////////// /************************************* * 指定具體節點,擷取標籤體內容 * @param doc * @param str_Label * @return *************************************/ public static String getElementFirstChildValueByNodeName(Element element){ String str_result=null; Text text = (Text) element.getFirstChild(); str_result=text.getNodeValue(); return str_result; } /************************************** * 指定節點名,從整個xml檔案中找出所有此名的節點的集合 * @param doc * @param str_Label * @param str_Attribute * @param str_Attribute_Value * @return ***************************************/ public static Vector getNodesByNodeName(Document doc,String str_NodeName){ Vector v_result=null; NodeList nl = doc.getElementsByTagName(str_NodeName); System.out.println("【提示】XML文檔中有"+nl.getLength()+"個<"+str_NodeName+">標籤"); int size=0; Node tempnode; if(nl != null){ size = nl.getLength(); v_result=new Vector(); for(int i=0; i<size; i++) { tempnode= nl.item(i); v_result.add(tempnode); } }else{ return null; } return v_result; } /***************************************************** * 指定具體節點,指定此節點下的其他節點名,找出所有為其名的子節點 * @param node * @param str_Label * @return Vector 的集合 *****************************************************/ public static Vector getNoesByNodeName(Node node,String str_NodeName){ Vector v_result=new Vector(); Node node_temp; NodeList nodelist = node.getChildNodes(); for(int i=0;i<nodelist.getLength();i++){ node_temp=nodelist.item(i); if(node_temp.getNodeName().equals(str_NodeName)) v_result.add(node_temp); } return v_result; } /************************************** * 匹配節點屬性名稱及屬性值去擷取特定的節點 * @param doc * @param str_Label * @param str_Attribute * @param str_Attribute_Value * @return ***************************************/ public static Node getNodeByAttributesValues(Document doc,String str_Label,String[] stra_Attributes,String[] stra_values){ Node node_result=null; NodeList nl = doc.getElementsByTagName(str_Label); System.out.println("【提示】xml文檔中有"+nl.getLength()+"個<"+str_Label+">標籤"); int size=0; Node tempnode; String []stra_att=new String[stra_Attributes.length]; if(nl != null) size = nl.getLength(); for(int i=0; i<size; i++) { tempnode= nl.item(i); int i_falg=0; for(int j=0;j<stra_att.length;j++){ stra_att[j]=KParseXML.getAttrValue(tempnode,stra_Attributes[j]); if(stra_att[j].equals(stra_values[j])){ i_falg++; } } if(i_falg==stra_Attributes.length) { System.out.println("【提示】傳遞的要與指定的標籤屬性比較的參數都滿足條件!"); node_result=tempnode; break; }else{ node_result=null; } } return node_result; } /************************* * 擷取某個節點某個屬性的值 * @param b * @param attrName * @return *************************/ public static String getAttrValue(Node node,String str_AttrName) { String str_result=null; NamedNodeMap nnm = node.getAttributes(); if(nnm == null) return null; for(int i=0; i<nnm.getLength(); i++) { if(nnm.item(i).getNodeName().equals(str_AttrName)) str_result = nnm.item(i).getNodeValue(); } return str_result; } /////////////////////////// //修改部分 ////////////////////////// /********************************* * 指定節點,指定屬性名稱,修改屬性值 * @param node * @param attrName * @param attrValue *********************************/ public static void setAttrValue(Node node,String attrName,String attrValue){ NamedNodeMap attributes = node.getAttributes(); if(attributes !=null) for(int i=0;i<attributes.getLength();i++){ String str_attributes_name = attributes.item(i).getNodeName(); if(str_attributes_name.equals(attrName)) attributes.item(i).setNodeValue(attrValue); } } /****************************** * 指定具體標籤,設定其標籤體的值 * @param element * @param str_value * @return ******************************/ public static boolean setElementValue(Element element,String str_value){ boolean b_result=true; try{ Node text = element.getFirstChild(); text.setNodeValue(str_value); }catch(Exception e){ b_result=false; e.printStackTrace(); } return b_result; } ///////////////////////////// //檔案處理部分 //////////////////////////// /********************************* * 在xml做好修改後,儲存xml檔案 * @param document * @param filename * @return ********************************/ public static boolean doc2XmlFile(Document document, String filename) { boolean flag = true; try { /** 將document中的內容寫入檔案中 */ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); /** 編碼 */ // transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(filename)); transformer.transform(source, result); } catch (Exception ex) { flag = false; ex.printStackTrace(); } return flag; } }
測試類別
//檔案3package com.kelsen.test;import java.net.URL;import java.util.Vector;import org.w3c.dom.Document;import org.w3c.dom.Element;import com.kelsen.beans.imagehelper.ImageToXML;import com.kelsen.beans.xmlhelper.KParseXML;public class XMLTest {public XMLTest(){ //testSetElementValue(); ImageToXMLTest(); ImageToXMLReadImage();}/*** 把圖片檔案轉成BASE64Encoder存入xml檔案中*/public void ImageToXMLTest(){ URL url_FileName=this.getClass().getClassLoader().getResource("com/kelsen/files/image/kelsen.jpg"); String str_FileName=url_FileName.getFile(); String content = ImageToXML.readImage(str_FileName); testSetElementValue(content);}/*** 從xml檔案取得圖片的BASE64Encoder內容從而建立本地圖片檔案*/public void ImageToXMLReadImage(){ String str_context=readImage(); ImageToXML.saveImage("D:/kkkk.jpg", str_context);}public String readImage(){ URL url_FileName = this.getClass().getClassLoader().getResource("com/kelsen/files/xml/kelsen.xml"); String str_FileName=url_FileName.getFile(); Document document = KParseXML.createDocument(str_FileName); Vector nodes_get = KParseXML.getNodesByNodeName(document, "guilin"); if(nodes_get!=null && nodes_get.size()>0){ Element element=(Element) nodes_get.get(0); return KParseXML.getElementFirstChildValueByNodeName(element); } return null;}/*** 標籤體內容修改測試**/public void testSetElementValue(String str_context){ URL url_FileName = this.getClass().getClassLoader().getResource("com/kelsen/files/xml/kelsen.xml"); String str_FileName=url_FileName.getFile(); Document document = KParseXML.createDocument(str_FileName); Vector nodes_get = KParseXML.getNodesByNodeName(document, "guilin"); if(nodes_get!=null && nodes_get.size()>0){ Element element=(Element) nodes_get.get(0); KParseXML.setElementValue(element,str_context); } KParseXML.doc2XmlFile(document, str_FileName); System.out.println("End 改完且儲存結束!");}public static void main(String args[]){ new XMLTest();}}
測試xml
<?xml version="1.0" encoding="GBK"?> <china name="中國"> <guangdong name="廣東省" description="省級標籤"> <guangzhou>廣州</guangzhou> <shenzhen>深圳</shenzhen> <shantou>汕頭</shantou> </guangdong> <guangxi name="廣西省" description="省級標籤"> <guilin>桂林</guilin> </guangxi> <hunan name="湖南省" description="省級標籤"> <changsha>長沙</changsha> <zhuzhou>株洲</zhuzhou> </hunan> </china>