Java basic knowledge traps (10) and java basic knowledge traps

Source: Internet
Author: User

Java basic knowledge traps (10) and java basic knowledge traps

This article is published on my blog.

Due to the tight schedule of the previous week, I failed to continue writing. Today I am making persistent efforts to concentrate and stick to it so that I am getting closer and closer to my goal! Today we are talking about the basic knowledge in java. We know that xml files are often used in java programming, and some of them support configuration by the framework itself, some of them are custom configurations, which requires us to have a better understanding of the xml principle. when loading the xml file conversion node element, there is a core: recursive call conversion. We can use the following method to check the source code related to 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 output includes:

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

This indicates that the DOMImplementationImpl class loads and converts xml files. Now I come to implement recursive output elements by myself, first look at the content of the mapred-default.xml file:

<?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 are stored       in this single well known place. If No value is set here, by default,      it is 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 is specified, the logs are stored in       output directory. The files are 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 see that configuration is an element, and the code should be judged if this element has no attribute. Below, we should pay special attention to the content that many people ignore, how many sub-elements are there? In xml, there are strict differences between spaces. Even if space is also an element, we should know the answer now: five, so we should be able to judge that it is blank in the code, this is the most fear of falling down during the interview. Now, after passing through the space element, there is the <property> element. This is the same as before, so we should implement it using recursion, when it comes to recursive methods, you must note that there must be a condition to exit. I know that other methods, such as getting sub-elements, should be used to obtain them. The code below is as follows:

/*** Parse XML file * @ param element Node Element */public static void parseXMLFile (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 {                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 running result is as follows:

<configuration isok="true">    <property>      <name>hadoop.job.history.location</name      <value></value      <description> If job tracker is static the history files are stored       in this single well known place. If No value is set here, by default,      it is 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 is specified, the logs are stored in       output directory. The files are stored in "_logs/history/" in the directory.      User can stop logging by giving the value "none".       </description    </property</configuration

The result is optimistic, so this example is implemented.


First come here this time. Keep recording!

Related Article

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.