Java 例項 - 獲取web.xml檔案中的資訊
阿新 • • 發佈:2021-11-01
XMLUtil執行檔案
public class XMLUtil { public static void main(String[] args) { String IP = XMLUtil.getIpName(); //讀取配置檔案中的引數 System.out.println(IP); String url = "http://" + IP + "/cc/json/mobile_tel_segment.htm?tel=13026194071"; System.out.println(url); } public staticString getIpName() { try { //建立文件物件 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; String cName=null; //檔案web.xml的絕對路徑 doc = builder.parse(newFile("web.xml檔案的絕對路徑")); //獲取包含類名的文字節點 //獲取包含圖表型別的文字節點 NodeList nl = doc.getElementsByTagName("IpName"); //通過設定不同的引數即可得到不同型別的圖表 Node classNode = nl.item(0).getFirstChild(); String chartType = classNode.getNodeValue().trim();return chartType; } catch(Exception e) { e.printStackTrace(); System.out.println("執行失敗,請檢查web.xml中是否有對應引數名稱"); return null; } } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <config> <IpName>192.168.24.123</IpName> </config>