Summary with Java's XML programming (reproduced)

zhaozj2021-02-16  70

An Analysis of the XML programming JAVA: Ningbo Ji Computer Technology Co., Ltd. R & D Xu Rongsheng issued a document time: 2004.07.02 XML structured as a global common language, more and more people of all ages, all kinds of development platforms (such as Microsoft Studio Series, Oracle series, Inprise Borland series, etc. also supports 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 6287555 Gender = "Men"> Zhang San 16 8273425

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:

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} {phone = s} PUBLIC VOID SETIT PUBLIC STRING GETSEX () {Return Sex;} public string getName () {Return Name;} public intert getage () {Return Age;} PUBLIC STRING GETPHONE ()}}}}}

After writing XML test classes, the author named XMLTEST, in order to read and write XML files, need to import as follows, "//", for note, the author's environment is JDK 1.3.1_04, in JDK 1.4.0 The test also passed the XML interpreter with Apache's crimson, can go to the Apache home page to upload. 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 show as below:

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 take root element "student flower name"

Element root = doc.getdocumentElement (); // Take "Student" Element List Nodelist StudentS = Root.getListSBytagname ("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 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, add 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 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. Remember my e-mail: nbdeveloper@hotmail.com.

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

New Post(0)