Last week because the time is relatively tight so failed to continue to write down, today, to concentrate on + adhere to the goal is getting closer! Less talk about the topic, today we still talk about the basic knowledge of Java, we know that many times in the Java program to use the XML file, some of the framework itself is supported by the configuration, and some are custom configuration, so that we need to understand this XML principle, Where the XML file transformation node element is loaded, there is a core: recursive call transformation. We can use the following methods to find out the source code for this implementation class:
1234 |
Documentbuilderfactory factory = documentbuilderfactory.newinstance ();D Ocumentbuilder build = Factory.newdocumentbuilder ();D ocument doc = build.parse (new File ("Mapred-default.xml")); System.out.println (Build.getdomimplementation (). toString ()); |
The outputs are:
1 |
Com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl |
Description This is the Domimplementationimpl class to load the XML file and convert it. Now I'm going to do it myself. To implement the recursive output element, first look at the contents of the Mapred-default.xml file:
lspacing= "0" >
1234567891011121314151617181920 |
<?xml version= "1.0"? ><configuration isok= "true" > <property> <name>hadoop.job.history.location</name> <value></value> <description> If Job Tracker is static The history files is stored in this, known place. If No value is set here, by Default, it are in the local file system at ${hadoop.log.dir }/history. </description> </property> <property> <name> Hadoop.job.history.user.location</name> <value></value> <description> User can specify a location to store the history files Of &n Bsp; &nbSp;a particular job. If Nothing was specified, the logs is stored in output directory. The files is stored in "_logs/history/" in the Directory. user can stop logging by giv ing the value "none". </description> </ Property></configuration> |
From the above file we can analyze: The configuration is an element, this element does not have the attribute code should be judged; The following is to pay special attention to a lot of people neglect, the child element is how many? In the XML is strictly the difference between the space, even if it is a space is an element, then we should now know the answer: 5, then in the code should be able to determine the blank, the most afraid of the fall in the interview. Now after the element of this space, and then the <property> element, and this is the same as before, then you should use the recursive implementation, when it comes to recursion method so pay attention must have a condition to exit And that's the one I know other things like getting a child element or something like that should have a special way to get it, here's the code:
123456789101112131415161718192021222324 |
/** * parsing xml file * @param element node elements */public static void Parsexmlfile (element Element) { system.out.print ("<" + Element.gettagname ()); namednodemap attributes = Element.getattributes (); if (Attributes! = null) { for (int i=0;i<attributes.getlength (); i++) { system.out.print ("" + Attributes.item (i). Getnodename () + "=\" "+ Attributes.item (i). GetNodeValue () + "\" "); } } System.out.print (">"); nodelist childNodes = Element.getchildnodes (); for (int i = 0; i < childnodes.getlength (); i++) { if ( Childnodes.item (i). Getnodetype () = = element.element_node ) { &nbsP; parsexmlfile (Element) Childnodes.item (i)); } else{ system.out.print (Childnodes.item (i). Gettextcontent ()); } } system.out.print ("</" + Element.gettagname ());} |
Main method:
12345678910111213 |
/** * @param args * @throws exception */ public Static void Main (string[] args) throws Exception { documentbuilderfactory factory = documentbuilderfactory.newinstance (); documentbuilder build = Factory.newdocumentbuilder (); document doc = build.parse (new File ("Mapred-default.xml"));// System.out.println (Build.getdomimplementation (). toString ()); element root = doc.getdocumentelement (); parsexmlfile (root); } |
The results of the operation are as follows:
12345678910111213141516171819 |
<configuration isok= "true" > <property> <name>hadoop.job.history.location</name <value></value <description> If Job Tracker is static the "history files" is stored in This is a known place. If No value is set here, by Default, it are in the Local file system at ${hadoop.lo g.dir}/history. </description </property <property> <name> hadoop.job.history.user.location</name <value></value <description> User can specify a location to store the history files Of &n Bsp; a particular job. If Nothing is specified,The logs is stored in output directory. The files is stored in "_logs/history/" in the Directory. user can stop logging By giving the value "none". </description </ Property</configuration |
The result is a good one, then this example is realized.
Come here first this time. Keep a record of every bit of drip!
- This article is from: Linux Tutorial Network
Java Basics Traps (10)