本文發表于本人博客。
上個星期由于時間比較緊所以未能繼續寫下去,今天再接再厲,專心 + 堅持這樣離目標就越來越近了!廢話少說說正題,今天我們還是來說說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>結果一看好,那么這個例子實現了。
這次先到這里。堅持記錄點點滴滴!
新聞熱點
疑難解答