JAVA simple XML parsing(JAVA简单的XML解析)
XML as a structured language universal, more and more popular, various platforms (such as Microsoft Studio series, Oracle series, Inprise Borland Series) are to support XML development as one of the slogan. Because of the e-government development in the early introduction of the XML, so many have tasted the sweetness, in many projects using XML data exchange information, eliminating a lot of trouble, do not make complicated data format, easy to use XML data expression, but also conducive to the front-line developers debugging.
I have previously published related articles, such as "Delphi analysis of XML programming" a text, interested readers can go to the Google network (le) to search, there are many media reprinted. Today, I would like to explore the Java programming in XML, and I hope it will help the new and old readers who are studying or want to learn XML programming.
In XML applications, the most commonly used and most practical is the reading and writing of XML files, so the author makes a brief analysis by reading and writing a simple XML file. Can first establish follows the structure of the XML file in any text editor, similar to the HTML structure, but the semantics of XML is very strict, the starting marks must be paired, such as "" student roster "" and "" / "student ros
ter" corresponding, how much space there is no need to care, but generally to the indented form of writing easy to read. This file is l, you can open the test in any browser that supports XML, and if you enter correctly, you can see the tree representation structure of the file in the browse. If you are still unfamiliar with the structure of XML, it is recommended
to look at "Delphi analysis of XML programming" in a text on the XML file instructions.
< XML, version=, 1, encoding=, GB2312 > >
< student roster >
< student sex = male >
< name > < / > name Li Hua
< age > >14</ > age >
< phone, >6287555</, phone >
< / > students
< student sex = male >
< > three < / > name name
< age > >16</ > age >
< phone, >8273425</, phone >
< / > students
< / student roster.
When the preparation was finished, then began to write the substantive JAVA code. To save the information read from the
XML file, you need to build a simple Bean to store student information, named StudentBean, as follows:
Public, class, StudentBean {
Private String sex; / / genderjava xml是什么
Private String name; / / the name of the student
Private int age; / / student age
Private String phone; / / telephone 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 phone;
}
}
The test class after writing XML, I take this class named XMLTest to read and write XML files, you need to import the following JAVA package "/ /" after the notes, the environment is JDK 1.3.1_04, in JDK 1.4.0 test by the XML interpreter, with Apache Crimson, you can go to the Apache home page to upload.
Import java.io.*; the //Java base package contains various IO operations
Import java.util.*; the //Java base package contains various standard data structure operations
l.parsers.*; //XML parser interface
Import org.w3c.dom.*; DOM implementation of //XML
Import XmlDocument; / / write XML file to use
To save multiple student information,
You have to use a collection class (not a collection in the sense only, and the set in JAVA is a collection of frames, including vectors, lists, hashes, etc.), where the Vector vector class is used. Defined in the XMLTest test class, named
student_Vector. Then define two methods, readXMLFile and writeXMLFile, to read and write operations. The code reads as follows:
Private, void, readXMLFile (String, inFile), throws, Exception

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。