XML file format:
Java code:
Import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileinputstream; import Java. io. filewriter; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. text. simpledateformat; import Java. util. date; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import javax. XML. parsers. parserconfigurationexception; impor T Org. w3C. dom. document; import Org. w3C. dom. nodelist; import Org. w3C. dom. element; import Org. w3C. dom. node; import Org. XML. sax. inputsource; import Org. XML. sax. saxexception; public class jxml {private string path; private file resultfolder; private string childname; private string attributename; documentbuilderfactory FAC = documentbuilderfactory. newinstance (); Public jxml (string path, string childname, string Attributename) {super (); this. path = path; this. childname = childname; this. attributename = attributename;} public void go () {file directory = new file (PATH); If (! Isdirectory (directory) {system. Out. println ("Path error or not folder"); return;} file [] files = getchildfiles (directory); If (! Hasfiles (Files) {system. out. println ("no file in the folder"); return;} createdirectory (PATH); For (INT I = 0; I <files. length; I ++) {// determine whether the XML file is an XML file // determine whether the XML file is empty readxml (files [I]);} public void readxml (File file) {documentbuilder builder = NULL; document DOC = NULL; filewriter fw = NULL; bufferedwriter BW = NULL; file resultfile = new file (resultfolder. getpath (). replace ("\", "/") + "/" + file. getname () + "result" + ". TXT" ); Try {resultfile. createnewfile (); fw = new filewriter (resultfile); BW = new bufferedwriter (FW); builder = FAC. newdocumentbuilder (); Doc = builder. parse (New inputsource (New bufferedreader (New inputstreamreader (New fileinputstream (file), "gb18030"); element root = Doc. getdocumentelement (); nodelist nodes = root. getchildnodes (); For (INT I = 0; I <nodes. getlength (); I ++) {node = nodes. item (I); If (n Ode. getnodename (). equals (childname) {BW. write (node. getattributes (). getnameditem (attributename ). getnodevalue (); BW. newline () ;}} BW. flush (); BW. close ();} catch (parserconfigurationexception e) {e. printstacktrace ();} catch (saxexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} public Boolean isdirectory (file directory) {If (directory. isdirectory () {return true;} return fa LSE;} public file [] getchildfiles (file directory) {return directory. listfiles ();} public void createdirectory (string path) {date data = new date (); simpledateformat tempdate = new simpledateformat ("mm minute SS seconds at HH on mm dd, YYYY"); string datetime = tempdate. format (data); string resultpath = path + "result" + datetime; file folder = new file (resultpath); If (! Folder. exists () {folder. mkdir ();} system. out. println ("Save the result in" + resultpath + "folder"); this. resultfolder = folder;} public Boolean hasfiles (file [] files) {If (! (Files. length = 0) {return true;} return false;} public static void main (string [] ARGs) {string Path = "D:/rulexml "; string childname = "rule"; string attributename = "name"; jxml = new jxml (path, childname, attributename); jxml. go ();}}
Note: The purpose of the code is to output the value of the name of the Rule node in the XML file.
Put the XML file in the folder. I put the XML file in the D:/rulexml folder, Path = folder path. Pay attention to the forward and backward slashes. The two lines in the text are incomplete.