Save images in XML format
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 {/*** convert the image to base64encoder * @ Param str_filename * @ return */public static string ReadImage (string str_filename) {bufferedinputstream Bis = NULL; byte [] bytes = NULL; 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);}/*** convert base64decoder to image * @ 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 ();}}}
Parse 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. ele Ment; import Org. w3C. dom. namednodemap; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; import Org. w3C. dom. text; public class kparsexml {/******************************** specify the file name, obtain the Document Object * @ Param str_filename * @ return ****************************** **/public static document createdocument (string str_filename) {document document_result = NULL; try {documentbuilderfactory documentbuilderf Acloud; documentbuilder; documentbuilderfactory = documentbuilderfactory. newinstance (); documentbuilder = documentbuilderfactory. newdocumentbuilder (); document_result = documentbuilder. parse (str_filename);} catch (exception e) {e. printstacktrace ();} return document_result ;} //////////////////////////////////////// // node, attribute, and value search part /////////////////////////////// ////////*************** * ********************** Specify a specific node, obtain the content of the TAG body * @ Param Doc * @ Param str_label * @ return ************************* * ***********/public static string getelementfirstchildvaluebynodename (element) {string str_result = NULL; text = (text) element. getfirstchild (); str_result = text. getnodevalue (); Return str_result ;} /*************************************** specify the node name, find the set of all nodes with this name from the XML file * @ 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 ("[prompt] the XML document contains" + NL. getlength () + "<" + str_nodename + "> tag"); 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 ;} /*************************************** * ************** specify a specific node, other node names under this node, find all the sub-nodes with their names * @ Param node * @ Param str_label * @ return vector's set ******************* * ********************************/public static vector getnoesbyn Odename (node, string str_nodename) {vector v_result = new vector (); node node_temp; 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 ;} /*************************************** match the node attribute name and attribute value to obtain a specific node * @ Param Doc * @ Param str_label * @ Param str_a Ttribute * @ 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 ("[prompt] the XML document contains" + NL. getlength () + "<" + str_label + "> tag"); int size = 0; node tempnode; string [] stra_at T = 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 ("[prompt] All parameters passed to be compared with the specified tag attribute meet the conditions! "); Node_result = tempnode; break;} else {node_result = NULL;} return node_result ;} /************************** obtain the value of an attribute of a node * @ Param B *@ param attrname * @ return ************************/public static string getattrvalue (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. it EM (I ). getnodename (). equals (str_attrname) str_result = NNM. item (I ). getnodevalue ();} return str_result ;} /// // modify the part ///////// //////////////////********************** * *********** specify a node, attribute name, modify the attribute value * @ Param node * @ Param attrname * @ Param attrvalue ************************** * *****/public static void setattrvalue (node, string attrname, string attrvalue) {namednodemap Tributes = 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 );}} /******************************* specify a specific tag, set the value of the TAG body * @ Param element * @ Param str_value * @ return *********************** * *****/public static Boolean setelementvalue (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 ;} /// // file processing part ////// ///////////////////////***************** * **************** after the XML is modified, save the XML file * @ Param document * @ Param filename * @ return ************************** * *****/public static Boolean doc2xmlfile (document, string filename) {Boolean flag = true; try {/** write the content in the document to the file */transformerfactory tfactory = transformerfactory. newinstance (); transformer = tfactory. newtransformer ();/** encoding * // 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 ;}}
Test class
// File 3 package 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 ();} /*** convert the image file to base64encoder and save it to the XML file */Public void imagetoxmltest () {URL url_fil Ename = This. getclass (). getclassloader (). getresource ("com/Kelsen/files/image/kelsen.jpg"); string str_filename = url_filename.getfile (); string content = imagetoxml. readImage (str_filename); testsetelementvalue (content);}/*** get the base64encoder content of the image from the XML file to create a local image file */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 = kparsexml. createdocument (str_filename); vector nodes_get = kparsexml. getnodesbynodename (document, "Guilin"); If (nodes_get! = NULL & nodes_get.size ()> 0) {element = (element) nodes_get.get (0); Return kparsexml. getelementfirstchildvaluebynodename (element);} return NULL;}/*** test the label body content modification **/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 = kparsexml. creat EDocument (str_filename); vector nodes_get = kparsexml. getnodesbynodename (document, "Guilin"); If (nodes_get! = NULL & nodes_get.size ()> 0) {element = (element) nodes_get.get (0); kparsexml. setelementvalue (element, str_context);} kparsexml.doc 2 xmlfile (document, str_filename); system. out. println ("End modified and saved! ");} Public static void main (string ARGs []) {New xmltest ();}}
Test XML
<? XML version = "1.0" encoding = "GBK"?> <China name = "China"> <Guangdong name = "Guangdong Province" Description = "Provincial tag"> <Guangzhou> Guangzhou </Guangzhou> <Shenzhen> Shenzhen </Shenzhen> <Shantou> Shantou </Shantou> </Guangdong> <Guangxi name = "" Description = "Provincial tag"> <Guilin> Guilin </Guilin> </Guangxi> <Hunan name = "Hunan Province" Description = "Provincial tag"> <Changsha> Changsha </Changsha> <Zhuzhou> Zhuzhou </Zhuzhou> </Hunan> </China>