XML is a global structured language, which is increasingly favored by people, and various development platforms (such as Microsoft Studio series, Oracle series, Inprise Borland series, etc.) also support XML development as one of the propaganda slogans. Since the e-government development of the author has an earlier introduction of XML, it tastes many sweets, using XML data exchange information in many projects, saves many troubles, do not use the data format of the lock, easy to use XML data easy Expression, also facilitates first-line developers track debugging. The author has previously published a related article, such as "Analysis of XML Programming in Delphi", interested readers can go to Google.com (http://www.google.com) to search, there are many media reprint. Today, I want to explore the XML programming in Java, I hope to help new and old readers who are being programmed to learn XML. In XML applications, the most commonly used most practical is the read and write of the XML file, so the author briefly analyzes through a simple XML file reading. The XML files that can be established in any text editor, similar to the HTML structure, but XML semantics is strict, and the starting mark must be paired, such as "
Code: XML Version = "1.0" Encoding = "GB2312"?>
After the preparation work is finished, the Java code that will begin to write a qualitative. In order to save information read from the XML file, you need to build a simple bean to save the student information, named StudentBean, the code is as follows: StudentBean.java
Code: Public class studentbean {private string sex; // Student gender private string name; // Student Name Private Int Age; // Student Age Private String Phone; // Phone Number Public Void SetSex (String S) {SEX = S; } public void setname (String s) {name = s;} public void setage (} public void setphone (string s) {phone = s;} public string getsex () {return sex;} Public string getname () {return name;} public string getPhone () {return pHONE;}} Write XML test class, testxml.java
Code: Import java.io. *; // Java foundation, contains various IO operations import java.util. *; // java basic package, including various standard data structures Import javax.xml.parsers. *; / / XML parser interface import org.w3c.dom. *; // XML DOM implementation import org.apache.crimson.tree.xmldocument; // Write XML file to use public class xmltest {Vector Student_vector; xmltest () { } // In order to save multiple student information, you have to rely on a collection class (not a collection of simple sense, the collection of java is the concept of the collection framework, including vector, list, hash table, etc.), here uses vector vector class. Define in the XMLTEST test class, named Student_Vector. Then define two methods ReadXmlFile and WriteXMLFILE to implement read and write operations.
Code is as follows: private void readXMLFile (String inFile) throws Exception {// prepare to parse XML, creating DocumentBuilderFactory instance, specify DocumentBuilder DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance (); DocumentBuilder db = null; try {db = dbf.newDocumentBuilder () } Catch (ParserConfigurationException PCE) {system.err.println (PCE); // Output exception information, then exit, with system.exit (1);} Document doc = null; try {doc = DB. PARSE (INFILE); DOMEXCEPTION DOM {system.err.println (Dom.getMessage ()); system.exit (1);} catch (ioexception ie) {system.err.println (IOE); system. Exit (1);} // The following is the whole process of parsing XML, relatively simple, first take root elements "Student Shuisu" Element Root = doc.getDocumentelement (); // Take "Student" Elements Nodelist Students = root.getElementsBytagname ("student"); for (int i = 0; i (Text) E.GETFIRSTCHILD (); StudentBean.setName (T.GETNODEVALUE ());} nodelist agent = student.getElementsBytagname ("age"); if (Ages.getLength () == 1) {ELEMENT E = (ELEMENT Ages.Item (0); Text T = (text) E.GETFIRSTCHILD (); studentBean.Setage (T.GETNodeValue ()));} nodelist phone = student.getlementsBytagname ("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 the DocumentBuilderFactory instance for parsing XML, specify DocumentBuilder DocumentBuilderFacto ry 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 is the process of establishing XML document content, first establish a root element "Student Relief" Element root = doc.createElement ("Student Relief"); // Root Element Add Document Doc.AppendChild (root); // Take the bean list for students information for (int i = 0; i ) {// Acquire each student's information StudentBean StudentBean = (StudentBean) Student_Vector.get (i); // Establish "Student" element, add to the root element ELEMENT = Doc.createElement ("Student"); student. SetAttribute ("Gender", StudentBean.getSex ()); root.appendchild (student); // Establish "Name" element, add it to the student below, with 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 ( "telephone"); student.appendChild (phone); Text tPhone = doc.createTextNode (studentBean.getPhone ( )); Phone.ap pendChild (tPhone);} // XML document to the specified output file FileOutputStream outStream = new FileOutputStream (outFile); OutputStreamWriter outWriter = new OutputStreamWriter (outStream); ((XmlDocument) doc) .write (outWriter, "GB2312"); OutWriter.close (); outstream.close ();} // finally join the test main function, as follows: public static void main (String [] args) throws exception {// Establish test instance xmltest xmltest = new xmltest (); / / Initialization Vector List XMLTest.student_Vector = New Vector (); System.out.Println ("Start Read Input.xml File");