本文发表于本人博客

上个星期由于时间比较紧所以未能继续写下去,今天再接再厉,专心 + 坚持这样离目标就越来越近了!废话少说说正题,今天我们还是来说说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. 工作流JBPM_day01:2-HelloWorld

    工作流JBPM_day01:2-HelloWorld 新建一个java工程Jbpm4.4 拷贝helloworld.jpdl.xml和helloworld.png文件出来压缩成helloworld.z ...

  2. ios 6以后,UILabel全属性

    一.初始化 1 UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)]; 2       3 [s ...

  3. upper()

    upper() 用于把字符串中的小写字母转换成大写字母 In [1]: str = "Hello World" In [2]: str.upper() Out[2]: 'HELLO ...

  4. 盒子模型 W3C中和IE中盒子的总宽度分别是什么

    W3C盒模型 总宽度 = margin-left + border-left + padding-left + width + padding-right + border-right + margi ...

  5. 为什么setinterval和settimeout越点击越快以及响应的解决办法

    setinterval大家都很了解,但是如果时间长的话,误差也会越来越大,所以我习惯上使用settimeout的递归,闲来没事,写了一个定时器的递归 <!DOCTYPE html> < ...

  6. 【MySQL】查询时强制区分大小写的方法

    MySQL默认的查询也不区分大小写.但作为用户信息,一旦用户名重复,又会浪费很多资源.再者,李逵.李鬼的多起来,侦辨起来很困难.要做到这一点,要么在建表时,明确大小写敏感(字段明确大小写敏感) sql ...

  7. 广义表操作 (ava实现)——广义表深度、广义表长度、打印广义表信息

    广义表是对线性表的扩展——线性表存储的所有的数据都是原子的(一个数或者不可分割的结构),且所有的数据类型相同.而广义表是允许线性表容纳自身结构的数据结构. 广义表定义: 广义表是由n个元素组成的序列: ...

  8. SWT/JFace开发遇到org.eclipse.core.runtime.IProgressMonitor问题的解决办法(转载)

    今日正在使用SWT和JFace开发一个系统,在搭建JFace平台时遇到了一个问题,运行HelloWorld程序抛出org.eclipse.core.runtime.IProgressMonitor的n ...

  9. 您需要安装旧 Java SE 6 运行环境才能打开“Eclipse”。

    mac删除jdk: sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk 旧版本sdk地址: http://www.oracle. ...

  10. Git的配置和使用

    eclipse中Git的配置 可以参考http://www.cnblogs.com/zhxiaomiao/archive/2013/05/16/3081148.html, http://blog.cs ...