Brief Analysis of the XML programming of Java

xiaoxiao2021-03-06  52

Analysis of the XML programming JAVA (to: beginners) http://www.chinaunix.net Author: Small cat Posted: 2003-11-06 18:31:38

Transfer from the Chinese Java Technology Network www.cn-java.com XML as a global structured language, which is increasingly favored by people, all kinds of development platforms, such as Microsoft Studio, Oracle Series, Inprise Borland Series, etc.) 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 "" and "" How many spaces don't have to be concerned, but it is generally written in a constant form, which is convenient for reading. Naming this file INPUT.XML, you can open the test in any support for XML, if you enter it correctly, you can see the tree representation of this file in the browse. If you also feel more unfamiliar with the XML structure, it is recommended to first look at "Analysis of XML Programming in Delphi" One Description of XML files. Li Hua 14 6287555 / Telephone> Zhang 3 - Name> 16 8273425

After the preparation work is finished, the Java code that will begin to write a qualitative. To save information read from the XML file, you need to build a simple bean to save the student information, named StudentBean, the code as follows: 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 getName () {return name;} public int getage () {Return Age;} public string getphone () {return phone;}} write XML test class, The author named XMLTEST, in order to read and write XML files, the author needs to import the Java package, "//", for the note, the author's environment is JDK 1.3.1_04, in JDK 1.4.0 test, XML explanation Use Apache's Crimson to go upload to the Apache home page. Import java.io. *; // Java foundation, containing various IO operations import java.util. *; // java basic package, including various standard data structures Import javax.xml.Parsers. *; // XML Analytical interface import org.w3c.dom. *; // XML DOM implementation import org.apache.crimson.tree.xmldocument; // write XML file to be used

In order to preserve multiple student information, you have to use 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 the 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);} catch (dom.err.println); 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 taking root elements "Student Shuisu" Element Root = Doc.getDocumentelement (); // Take "Student" element list nodelist students = root.get.GtelementsBytagname ("student"); for (int i = 0; i

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 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); System.exit (1); Document Doc = NULL; DOC = DB.NEWDocument ();

// The following is the process of establishing the content of XML document, 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

Element ageElement ("age"); student.Appendchild (age); Text Tage = doc.createtextNode (String.Valueof (studentBean.getage ()); age.Appendchild (TAGE);

Element phone = doc.createElement; student.Appendchild (phone); text tphone = doc.createtextNode (studentBean.getphone ()); phone.Appendchild (tphone);} // output XML document to the specified file FileOutputStream outStream = new FileOutputStream (outFile); OutputStreamWriter outWriter = new (outStream) OutputStreamWriter; ((XmlDocument) doc) .write (outWriter, "GB2312"); outWriter.close (); outStream.close ();}

Finally, the test main function is added, 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 Files); XMLTest.Readxmlfile (" Input.xml ");

System.out.println ("After reading, start writing out.xml files); Xmltest.WriteXmlFile (" Output.xml "); system.out.println (" write completion ");}

Ok, save StudentBean and XMLTest, save the Input.xml to the working directory. If you enter a very careful, you can see "Write Finish", you can see the "write completion", go to the output.xml file and the input.xml file is not the same. If you find any questions during the debugging process, please contact the author through e-mail: nbdeveloper@hotmail.com.

转载请注明原文地址:https://www.9cbs.com/read-115733.html

New Post(0)