{"id":507,"date":"2021-05-16T02:11:08","date_gmt":"2021-05-16T02:11:08","guid":{"rendered":"http:\/\/emacslisp.com\/?p=507"},"modified":"2021-05-16T02:13:29","modified_gmt":"2021-05-16T02:13:29","slug":"log4j-src-parse-log4j-xml-building-documentbuilder","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=507","title":{"rendered":"log4j src &#8211; parse log4j.xml building DocumentBuilder"},"content":{"rendered":"<p>log4j uses DocumentBuilder to parse log4j.xml<\/p>\n<p>here is example code which is same as log4j original source code.<\/p>\n<pre lang=\"java\" line=\"1\"> \r\n\r\npackage main;\r\n\r\nimport java.io.*;\r\nimport java.net.*;\r\n\r\nimport javax.xml.parsers.DocumentBuilder;\r\nimport javax.xml.parsers.DocumentBuilderFactory;\r\nimport javax.xml.parsers.ParserConfigurationException;\r\n\r\nimport org.w3c.dom.Document;\r\nimport org.w3c.dom.Node;\r\nimport org.w3c.dom.NodeList;\r\nimport org.xml.sax.InputSource;\r\nimport org.xml.sax.SAXException;\r\n\r\npublic class DocumentBuilderEntry {\r\n\r\n\tpublic static void loop(Node node) {\r\n\t    \/\/ do something with the current node instead of System.out\r\n\t    System.out.println(node.getNodeName());\r\n\r\n\t    NodeList nodeList = node.getChildNodes();\r\n\t    for (int i = 0; i < nodeList.getLength(); i++) {\r\n\t        Node currentNode = nodeList.item(i);\r\n\t        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {\r\n\t            \/\/calls this method for all the children which is Element\r\n\t        \tloop(currentNode);\r\n\t        }\r\n\t    }\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\tURL url = DocumentBuilderEntry.class.getResource(\"log4j.xml\");\r\n\t\tDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); \r\n\t\ttry {\r\n\t\t\tDocumentBuilder parser = factory.newDocumentBuilder();\r\n\t\t\tURLConnection uConn = url.openConnection();\r\n\t\t\t\r\n\t\t\tuConn.setUseCaches(false);\r\n\t\t\tInputStream stream = uConn.getInputStream();\r\n\t\t\ttry {\r\n\t\t\t  InputSource src = new InputSource(stream);\r\n\t\t\t  src.setSystemId(url.toString());\r\n\t\t\t  Document doc = parser.parse(src);\r\n\t\t\t  loop(doc.getDocumentElement());\r\n\t\t\t} catch (SAXException e) {\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t} finally {\r\n\t\t\t  stream.close();\r\n\t\t\t}\r\n\t\t} catch (IOException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t} catch (ParserConfigurationException e1) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te1.printStackTrace();\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>log4j uses DocumentBuilder to parse log4j.xml here is example code which is same as log4j original source code. package main; import java.io.*; import java.net.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class DocumentBuilderEntry { public static void loop(Node node) { \/\/ do something with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=507"}],"version-history":[{"count":1,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":508,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/507\/revisions\/508"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}