When I first came into contact with dom4j, I felt this thing was mysterious, because although I knew about XML files before, I never used it. I always felt that XML is definitely not easy to operate. When I learned that dom4j can easily read XML files, how simple is it to read XML files using dom4j? Let me take a look.
First, let's take a look at the structure of the XML file:
<?xml version="1.0" encoding="utf-8"?><ACCESOS><item><SOCIO><NUMERO>00045050</NUMERO><REPOSICION>0</REPOSICION><NOMBRE>MOISES MORENO</NOMBRE><TURNOS><LU>T1</LU><MA>T2</MA><MI>T3</MI><JU>T4</JU><VI>T5</VI><SA>T6</SA><DO>T7</DO></TURNOS></SOCIO></item><item><SOCIO><NUMERO>00045051</NUMERO><REPOSICION>0</REPOSICION><NOMBRE>RUTH PENA</NOMBRE><TURNOS><LU>S1</LU><MA>S2</MA><MI>S3</MI><JU>S4</JU><VI>S5</VI><SA>S6</SA><DO>S7</DO></TURNOS></SOCIO></item></ACCESOS>
To read an XML file using dom4j, you only need two steps:
Step 1: Introduce the JAR file, introduce the dom4j-1.6.1.jar file and jaxen-1.1-beta-6.jar File
Step 2: write a few lines of code. The specific code is as follows:
Import Java. io. file; import Java. util. iterator; import Java. util. list; import Org. dom4j. document; import Org. dom4j. extends entexception; import Org. dom4j. element; import Org. dom4j. io. saxreader; public class readxml {public static void main (string [] Arg) {try {// XML file in E:/test01.xmldocument Doc = new saxreader (). read (new file ("E:/test01.xml"); List <element> itemlist = Doc. selectnodes ("/ACCESOS/item/socio"); For (iterator <element> iter = itemlist. iterator (); ITER. hasnext ();) {element = ITER. next (); system. out. println ("numero =" + element. elementtext ("numero");} catch (incluentexception e) {e. printstacktrace ();}}}
OK. Now you can read the content in the XML file. Let's see if the result is correct:
NUMERO =00045050NUMERO =00045051
Perfect! The result is correct. So far, we have used only a few lines of code, and the data in XML has been encapsulated into the object. There is almost no difference between retrieving the data in XML and retrieving the data in the array, this is the power of dom4j. With dom4j, when I tried to operate XML files again, I was so impatient that my head was not big. Reading ten XML files in one breath was a small case.
People have a fear of the unknown, just like getting started with dom4j, dom4j, and XPath. These new terms suddenly appear in front of us, there is a kind of subconscious to be avoided immediately. Actually, it is just a few new terms. Most of the time, what stops us from moving forward is not the unknown, but our fear of the unknown. As long as we overcome this fear, you will receive unexpected surprises.
In today's society, especially in the computer world, if you keep resisting new things, you will soon be eliminated and become an antique. The idea of advancing with the times is particularly prominent in the computer world. Living in this age, if you do not pay attention to certain strategies, you may not learn quickly. In this environment, if you study new knowledge and want to understand it clearly, then in the animal world running competition, your learning speed will be comparable to that of snail bait. Therefore, a lot of things can be used. To be good at things, you must learn to stand on the shoulders of giants to consider the problem. The learning method is very practical and effective. Of course, I do not deny the importance of research. The spirit of exploration is still essential,? If we spend time exploring things that are not necessary to think about "things should be learned and things should be studied", I feel that this is more conducive to our development.
There is a balance between "wonton jujube" and "Research and Exploration. Learning to find this balance point is more important than learning specific technologies.
The road is rough, boys. Come on!