Example of parsing XML file in Java -- writing the attribute values of reading subnodes to the corresponding file

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.