Here you can find the source of tomcatPort(String fileName)
public static Integer tomcatPort(String fileName)
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; public class Main { public static Integer tomcatPort(String fileName) { String xpathExpression = "/Server/Service/Connector[@protocol='HTTP/1.1']/@port"; String value = string(fileName, xpathExpression); try {/*from w ww . ja va 2 s . c om*/ int parseInt = Integer.parseInt(value); return parseInt; } catch (NumberFormatException e) { e.printStackTrace(); return null; } } public static String string(String fileName, String xpathExpression) { TransformerFactory transFact = TransformerFactory.newInstance(); try { Transformer transFormer = transFact.newTransformer(); DOMResult dom = new DOMResult(); transFormer.transform(new StreamSource(new FileInputStream( new File(fileName))), dom); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expression = xpath.compile(xpathExpression); String value = (String) expression.evaluate(dom.getNode(), XPathConstants.STRING); return value; } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; } }