Java XML String Transform tomcatPort(String fileName)

Here you can find the source of tomcatPort(String fileName)

Description

tomcat Port

License

Apache License

Declaration

public static Integer tomcatPort(String fileName) 

Method Source Code

//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;
    }
}

Related

  1. toElement(String xml)
  2. toHexString(byte[] array)
  3. toHTML(String xml)
  4. toJson(String xml)
  5. toLCCNDisplay(String packedLCCN)
  6. toString(Collection c, String separator)
  7. toString(Source input)
  8. write(Node node, OutputStream out, String... props)
  9. writeElement(Element element, String fileName)