【Java】【XML】xPath [2] ~XMLの参照~

サンプル

SampleXml.java

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class SampleXml {
   public static void main(String[] args)
         throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        // DOMパーサ用ファクトリの生成
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // DOM Documentインスタンス用ファクトリの生成
        DocumentBuilder builder = factory.newDocumentBuilder();
        // 解析とDocumentインスタンスの取得
        Document document = builder.parse(new File("conf/server.xml"));
        
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        
        Node nodeForShutdown = (Node) xpath.evaluate("Server", document, XPathConstants.NODE);
        Node nodeForShutdownPort = nodeForShutdown.getAttributes().getNamedItem("port");
        System.out.println("Shutdown Port : " + nodeForShutdownPort.getNodeValue());

        Node nodeForSsl = (Node) xpath.evaluate("Server/Service/Connector[@protocol='HTTP/1.1' and @SSLEnabled='true' and @scheme='https']", document, XPathConstants.NODE);
        Node nodeForSslPort = nodeForSsl.getAttributes().getNamedItem("port");
        System.out.println("SSL Port : " + nodeForSslPort.getNodeValue());
        
        Node nodeForHttp = (Node) xpath.evaluate(
              String.format("Server/Service/Connector[@protocol='HTTP/1.1' and @redirectPort='%s']",
                    nodeForSslPort.getNodeValue()), document, XPathConstants.NODE);
        Node nodeForHttpPort = nodeForHttp.getAttributes().getNamedItem("port");
        System.out.println("HTTP Port : " + nodeForHttpPort.getNodeValue());
        
        Node nodeForAjp = (Node) xpath.evaluate(
              String.format("Server/Service/Connector[@protocol='AJP/1.3' and @redirectPort='%s']",
                    nodeForSslPort.getNodeValue()), document, XPathConstants.NODE);
        Node nodeForAjpPort = nodeForAjp.getAttributes().getNamedItem("port");
        System.out.println("AJP Port : " + nodeForAjpPort.getNodeValue());
   }
}

conf/server.xml

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
  </Service>
</Server>

出力結果

Shutdown Port : 8005
SSL Port : 8443
HTTP Port : 8080
AJP Port : 8009