本文发表于本人博客

上个星期由于时间比较紧所以未能继续写下去,今天再接再厉,专心 + 坚持这样离目标就越来越近了!废话少说说正题,今天我们还是来说说java中比较基础的知识,大家知道编写java程序中很多时候都用到了xml文件,有些是框架本身支持配置的,有些是自定义配置的,这样就要求我们要对这个xml原理要比较了解,其中加载xml文件转换节点元素时有个核心:递归调用转换。我们可以通过下面方法来查查这个实现类有关的源码:

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder build = factory.newDocumentBuilder();
Document doc = build.parse(new File("mapred-default.xml"));
System.out.println(build.getDOMImplementation().toString());

输出其中有:

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

说明这是由DOMImplementationImpl类来加载xml文件并转化的。现在我就来自己实现递归输出元素,先看下mapred-default.xml这个文件的内容:

<?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>

从上面的这个文件我们可以分析出:configuration是一个元素,这个元素没属性代码应该要判断;下面这个要特别注意很多人忽视掉的,其子元素究竟是有几个?在xml中是严格区别空格的,即使就是空格也是一个元素,那现在我们应该知道答案了吧:5个,那么在代码中应该可以判断是空白,这个最怕在面试时候跌倒了。那现在过了这个空格的元素后,接着就是<property>元素了,这个又跟之前一样哦,那么就应该使用递归来实现,说到递归方法那么就要注意必须有个条件退出;那着个我知道其他的比如获取子元素之类的应该会有专门的方法获取得到,下面看代码:

    /**
* 解析XML文件
* @param element 节点元素
*/
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方法:

    /**
* @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);
}

运行结果如下:

<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>

结果一看好,那么这个例子实现了。

这次先到这里。坚持记录点点滴滴!

Java基础知识陷阱(十)的更多相关文章

  1. Java基础知识陷阱系列

    Java基础知识陷阱系列 今天抽空把Java基础知识陷阱有关的文章汇总于此,便于大家查看. Java基础知识陷阱(一) Java基础知识陷阱(二) Java基础知识陷阱(三) Java基础知识陷阱(四 ...

  2. Java基础知识陷阱(九)

    本文发表于本人博客. 今天我来说说关于JAVA多线程知识,有错误请指出.大家都知道JAVA在服务端上处理也有很大优势,很多公司也有在服务器跑JAVA进程,这说明JAVA在处理这个多线程以及并发下也有一 ...

  3. Java基础知识陷阱(二)

    本文发表于本人博客. 上次说了一些关于字符串的知识,都是比较基础的,那这次也说下关于对象地址问题,比如传参.先看下面代码: public void changeInt(int a){ a = ; } ...

  4. Java基础知识陷阱(七)

    本文发表于本人博客. 上次说了下HashSet和HashMap之间的关系,其中HashMap这个内部有这么一句: static final float DEFAULT_LOAD_FACTOR = 0. ...

  5. Java基础知识陷阱(六)

    本文发表于本人博客. 上次说了下equals跟==的问题,今天再来认识一下这个equals()跟hasCode().上次的代码如下: class Person{ public String name; ...

  6. Java基础知识陷阱(四)

    本文发表于本人博客. 今天我们来说说关于java继承以及反射有关的问题,大家先看下下面代码,试问可以编译通过不,为什么具体说说原因? public class Test{ public static ...

  7. Java基础知识陷阱(三)

    本文发表于本人博客. 之前都讲了有关字符串的陷阱,那今天来说下关于静态这个东西,这分为静态变量.静态方法,先看下面的代码请问结果输出是什么?: class Person01{ private stat ...

  8. Java基础知识陷阱(一)

    本文发表于本人博客. 事隔好多年了,重新拿起来Java这门语言,看似熟悉其实还很陌生,想想应该梳理下顺便提高下自己.这次先来看看Java里面的String这个对象. 经典的先看下面一段代码,请问最终创 ...

  9. Java基础知识陷阱(八)

    本文发表于本人博客. 这次我来说说关于&跟&&的区别,大家都知道&是位运算符,而&&是逻辑运算符,看下面代码: public static void m ...

随机推荐

  1. java.util.logging.Logger使用具体解释

    java.util.logging.Logger不是什么新奇东西了,1.4就有了,但是由于log4j的存在,这个logger一直沉默着,事实上在一些測试性的代码中,jdk自带的logger比log4j ...

  2. Unity中SendMessage和Delegate效率比较

    网上直接搜的代码.需要的使用也简单,所以就不过多说明. 但是网上都说,他们之间的差距,delegate比较快,效果高.怎么个高法呢?还是自己来测试下时间. 故此, 个人之用来比较下时间差别. 一.直接 ...

  3. 【LeetCode】Pascal's Triangle II (杨辉三角)

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. Java三方---->excel框架之POI的使用一

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.pdf框架之IText的使用,参见我的博客:Java ...

  5. JavaWeb温习之Session对象

    1. Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服 ...

  6. object.prototype.call

    object.prototype.call /* * object.prototype.call * @ 当一个object没有某个方法,但是其他的有,我们可以借助call或apply用其它对象的方法 ...

  7. ansible批量验证密码

    author: headsen chen date: 2018-08-31  20:45:49 综合比较salt-ssh,ansible .sshpass等批量验证100台机器的密码,最好推荐用ans ...

  8. [SQL] 获取 Microsoft SQL Server 2008 的数据表结构

    then d.name else '' end , 表说明 then isnull(f.value,'') else '' end , 字段序号 = a.colorder , 字段名 = a.name ...

  9. c++Template 的辨析

    1.在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢? 答:class用于定义类,在模板引入c++后,最初定义模板的方 ...

  10. poj1463 Strategic game【树形DP】

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 9582   Accepted: 4516 De ...