Java Basics Traps (10)

Source: Internet
Author: User

This article is published in my blog .

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:

        Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();        Documentbuilder build = Factory.newdocumentbuilder ();        Document doc = build.parse (new File ("Mapred-default.xml"));        System.out.println (Build.getdomimplementation (). toString ());

The outputs are:

Com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl

This is the Domimplementationimpl class to load the XML file and convert it. now I'm going to implement the recursive output element, first look at the contents of this file Mapred-default.xml:

<?xml version= "1.0"? ><configuration isok= "true" > <property > <name>hadoop.job.history.location</name> <value></value> <description> I F Job Tracker is the "The history files" is stored in the "this" known place.      If No value is set here, by default, it was in the local file system at ${hadoop.log.dir}/history. </description> </property> <property> <NAME>HADOOP.JOB.HISTORY.USER.LOCATION</NAME&G      T <value></value> <description> User can specify a location to store the history files of a par Ticular 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 giving 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; here's to pay special attention to what many people neglect, in the XML is strictly the difference between the space, even if it is a space is an element, we should now know the answer: 5, then in the code should be judged to be blank, the most afraid to 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 should have a specific way to get it, see the code below:

    /**     * Parse 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
   ) {                parsexmlfile (Element) Childnodes.item (i));            }            else{                System.out.print (Childnodes.item (i). Gettextcontent ());            }        }        System.out.print ("</" + element.gettagname ());    }

Main method:

    /**     * @param args     * @throws Exception * * Public    static void Main (string[] args) throws Exception {                do Cumentbuilderfactory 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:

<configuration isok= "true" >    <property>      <name>hadoop.job.history.location</name      <value></value      <description> If Job Tracker is static the history files were stored in this       sin Gle well known place. If No value is set here, by default, it was 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
   
    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 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!

Java Basics Traps (10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.