Import java. Io. *; // Basic Java package, including various Io operations Import java. util. *; // Basic Java package, including various standard data structure operations Import javax. xml. parsers. *; // XML Parser Interface Import org. W3C. Dom. *; // Dom Implementation of XML Import org. Apache. Crimson. Tree. xmldocument; // used to write XML files Public class xmltest { Vector student_vector; Xmltest (){ } // To save multiple student information, you must use a collection class (not a set in a simple sense, but a set in Java is the concept of a collection framework, contains vectors, lists, hash tables, etc.). The Vector class is used here. Defined in the xmltest test class and named student_vector. Then we define two methods: readxmlfile and writexmlfile to implement read/write operations. The Code is as follows: Private void readxmlfile (string infile) throws exception { // Prepare for XML parsing, create a documentbuilderfactory instance, and specify documentbuilder Documentbuilderfactory DBF = documentbuilderfactory. newinstance (); Documentbuilder DB = NULL; Try { DB = DBF. newdocumentbuilder (); } Catch (parserconfigurationexception PCE ){ System. Err. println (PCE); // output exception information when an exception occurs, and then exit, the same below System. Exit (1 ); } Document Doc = NULL; Try { Doc = dB. parse (infile ); } Catch (domexception DOM ){ System. Err. println (DOM. getmessage ()); System. Exit (1 ); } Catch (ioexception IOE ){ System. Err. println (IOE ); System. Exit (1 ); } // The following is the entire process of parsing XML, which is relatively simple. First, take the root element "student roster" Element root = Doc. getdocumentelement (); // Retrieve the "student" element list Nodelist students = root. getelementsbytagname ("student "); For (INT I = 0; I <students. getlength (); I ++ ){ // Obtain each "student" element in sequence Element student = (element) students. item (I ); // Create a student's bean instance Studentbean = new studentbean (); // Obtain the Gender attribute of the student Studentbean. setsex (student. getattribute ("gender ")); // Obtain the "name" element, which is similar Nodelist names = student. getelementsbytagname ("name "); If (names. getlength () = 1 ){ Element E = (element) names. Item (0 ); Text t = (text) E. getfirstchild (); Studentbean. setname (T. getnodevalue ()); } Nodelist ages = student. getelementsbytagname ("Age "); If (ages. getlength () = 1 ){ Element E = (element) ages. Item (0 ); Text t = (text) E. getfirstchild (); Studentbean. setage (integer. parseint (T. getnodevalue ())); } Nodelist phones = student. getelementsbytagname ("phone "); If (phones. getlength () = 1 ){ Element E = (element) phones. Item (0 ); Text t = (text) E. getfirstchild (); Studentbean. setphone (T. getnodevalue ()); } Student_vector.add (studentbean ); } } Private void writexmlfile (string OUTFILE) throws exception { // Prepare for XML parsing, create a documentbuilderfactory instance, and specify documentbuilder Documentbuilderfactory DBF = documentbuilderfactory. newinstance (); Documentbuilder DB = NULL; Try { DB = DBF. newdocumentbuilder (); } Catch (parserconfigurationexception PCE ){ System. Err. println (PCE ); System. Exit (1 ); } Document Doc = NULL; Doc = dB. newdocument (); // The following describes how to create the XML document content. First, create the root element "student roster" Element root = Doc. createelement ("student roster "); // Add the document to the root element Doc. appendchild (Root ); // Retrieve the bean list of Student Information For (INT I = 0; I <student_vector.size (); I ++ ){ // Obtain the information of each student in sequence Studentbean = (studentbean) student_vector.get (I ); // Create a "student" element and add it to the root element Element student = Doc. createelement ("student "); Student. setattribute ("gender", studentbean. getsex ()); Root. appendchild (student ); // Create a "name" element and add it to the student, the same below Element name = Doc. createelement ("name "); Student. appendchild (name ); Text tname = Doc. createtextnode (studentbean. getname ()); Name. appendchild (tname ); Element age = Doc. createelement ("Age "); Student. appendchild (AGE ); Text Tage = Doc. createtextnode (string. valueof (studentbean. Getage ())); Age. appendchild (Tage ); Element phone = Doc. createelement ("phone "); Student. appendchild (phone ); Text tphone = Doc. createtextnode (studentbean. getphone ()); Phone. appendchild (tphone ); } // Output the XML document to the specified file Fileoutputstream outstream = new fileoutputstream (OUTFILE ); Outputstreamwriter outwriter = new outputstreamwriter (outstream ); (Xmldocument) Doc). Write (outwriter, "gb2312 "); Outwriter. Close (); Outstream. Close (); } // Add the test main function as follows: Public static void main (string [] ARGs) throws exception { // Create a test instance Xmltest = new xmltest (); // Initialize the vector list Xmltest. student_vector = new vector (); System. Out. println ("starting to read the input. xml file "); Xmltest. readxmlfile ("input. xml "); System. Out. println ("read finished, start to write the output. xml file "); Xmltest. writexmlfile ("output. xml "); System. Out. println ("Write completed "); System. In. Read (); } } |