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> <?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> it takes only two steps to read the XML file using dom4j:
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.XML Document 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 element = iter. next (); System. out. println ("NUMERO =" + element. elementText ("NUMERO");} catch (incluentexception e) {e. printStackTrace () ;}} 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.XML Document 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 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 =00045050 NUMERO =00045051 NUMERO =00045050NUMERO =00045051
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.
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. Many times, what stops us from moving forward is not something 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 truth is rough, boys. Come on!