Brief Analysis of the XML programming of Java

xiaoxiao2021-03-18  216

The author presses: Individual thinks this article is easy to understand, it is recommended. 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 also published a related article, such as "Analysis of XML Programming in Delphi", interested readers can go to Google (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 (INT A) {AGE = a;} public void setphone (string s) {phone = s;} public string getsex () {return sex;} public string getName () {return name;} public int getage () {Return Age;} public String getPhone () {return;}} Write XML test class, the author named XMLTEST, in order to read and write XML files, need to import as follows, "//", for the note, the author's environment is JDK 1.3.1_04, test in JDK 1.4.0 also passed, XML interpreter with Apache's crimson, can go to the Apache home page to upload. You need to go to the URL http://xml.apache.org/dist/crimson/ to download Apache Crimson and add the acquired crimson.jar file to the environment variable ClassPath. 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 save multiple student information, you have to with a collection class (Not a collection of simple sense, Java's collection is the concept of 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: public 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);} // below is the whole process of analyzing XML, relatively simple, first take the root element "Student Shuisu" Element Root = Doc.getDocumentelement (); // Take "Student" Elements Nodelist Students = Root.get.GtelementsBytagname ("Student"); for (int I = 0; I

studentBean.setPhone (t.getNodeValue ());} student_Vector.add (studentBean);}} public 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 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

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

New Post(0)