国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java基礎知識陷阱(十)

2019-11-14 22:32:09
字體:
來源:轉載
供稿:網友
java基礎知識陷阱(十)

本文發表于本人博客。

上個星期由于時間比較緊所以未能繼續寫下去,今天再接再厲,專心 + 堅持這樣離目標就越來越近了!廢話少說說正題,今天我們還是來說說java中比較基礎的知識,大家知道編寫java程序中很多時候都用到了xml文件,有些是框架本身支持配置的,有些是自定義配置的,這樣就要求我們要對這個xml原理要比較了解,其中加載xml文件轉換節點元素時有個核心:遞歸調用轉換。我們可以通過下面方法來查查這個實現類有關的源碼

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        DocumentBuilder build = factory.newDocumentBuilder();        Document doc = build.parse(new File("ma

輸出其中有:

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>

結果一看好,那么這個例子實現了。

這次先到這里。堅持記錄點點滴滴!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福鼎市| 商河县| 克拉玛依市| 得荣县| 炉霍县| 漠河县| 丰县| 鄂尔多斯市| 五大连池市| 肇庆市| 友谊县| 攀枝花市| 博野县| 阳城县| 鹤山市| 泰兴市| 宁国市| 东明县| 聊城市| 西平县| 玛纳斯县| 石林| 甘洛县| 保德县| 南阳市| 阆中市| 剑河县| 云安县| 黑山县| 兴化市| 德兴市| 亚东县| 门头沟区| 奉新县| 岑巩县| 长汀县| 姚安县| 亳州市| 尼玛县| 榆树市| 潜山县|