【Java】【XML】 xPath ~XMLの書き換え~

 ■ はじめに

https://dk521123.hatenablog.com/entry/2014/07/03/000100

のつづき。
参照ができたので、今度は値を書き換えてみる。

 ■ サンプル

 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.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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, TransformerException {
      // DOMパーサ用ファクトリの生成
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      // DOM Documentインスタンス用ファクトリの生成
      DocumentBuilder builder = factory.newDocumentBuilder();
      // 解析とDocumentインスタンスの取得
      File file = new File("conf/server.xml");
      Document document = builder.parse(file);

      XPathFactory xPathFactory = XPathFactory.newInstance();
      XPath xpath = xPathFactory.newXPath();
        
      Node nodeForSsl = (Node) xpath.evaluate(
          "Server/Service/Connector[@SSLEnabled='true' and @scheme='https']",
          document, XPathConstants.NODE);
      Node nodeForSslPort = nodeForSsl.getAttributes().getNamedItem("port");
      System.out.println("Current SSL Port : " + nodeForSslPort.getNodeValue());
      
      nodeForSslPort.setNodeValue("18443");;
      System.out.println("New SSL Port : " + nodeForSslPort.getNodeValue());
      
      /*
       * DOMオブジェクトを文字列として出力
       */ 
      TransformerFactory transFactory = TransformerFactory.newInstance(); 
      Transformer transformer = transFactory.newTransformer(); 
      transformer.transform(new DOMSource(document), new StreamResult(file)); 
   }
}

 conf/server.xml (実行前(修正前)。SSLのポートが「8443」)

<?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="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>

出力結果

 コンソール画面

Current SSL Port : 8443
New SSL Port : 18443

 conf/server.xml (実行後(修正後)。SSLのポートが「18443」に書き換わっている)

<?xml version="1.0" encoding="utf-8" standalone="no"?><Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    <Connector SSLEnabled="true" clientAuth="false" maxThreads="150" port="18443" protocol="org.apache.coyote.http11.Http11Protocol" scheme="https" secure="true" sslProtocol="TLS"/>
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
    <Engine defaultHost="localhost" name="Catalina">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
      </Host>
    </Engine>
  </Service>
</Server>

 参考文献

http://www6.airnet.ne.jp/manyo/xml/dom_java/step23.html
http://blog.goo.ne.jp/xmldtp/e/d7a9634935183c6a58745d4aedd8c775

 関連記事

XML API の使い分けについて
https://dk521123.hatenablog.com/entry/2015/06/06/100247

 はじめに http://blogs.yahoo.co.jp/dk521123/33606218.html のつづき。 参照ができたので、今度は値を書き換えてみる。

 サンプル  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.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; 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, TransformerException { // DOMパーサ用ファクトリの生成 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // DOM Documentインスタンス用ファクトリの生成 DocumentBuilder builder = factory.newDocumentBuilder(); // 解析とDocumentインスタンスの取得 File file = new File("conf/server.xml"); Document document = builder.parse(file);

  XPathFactory xPathFactory = XPathFactory.newInstance();
  XPath xpath = xPathFactory.newXPath();

  Node nodeForSsl = (Node) xpath.evaluate(
      "Server/Service/Connector[@SSLEnabled='true' and @scheme='https']",
      document, XPathConstants.NODE);
  Node nodeForSslPort = nodeForSsl.getAttributes().getNamedItem("port");
  System.out.println("Current SSL Port : " + nodeForSslPort.getNodeValue());

  nodeForSslPort.setNodeValue("18443");;
  System.out.println("New SSL Port : " + nodeForSslPort.getNodeValue());

  /*
   * DOMオブジェクトを文字列として出力
   */ 
  TransformerFactory transFactory = TransformerFactory.newInstance(); 
  Transformer transformer = transFactory.newTransformer(); 
  transformer.transform(new DOMSource(document), new StreamResult(file)); 

} }  conf/server.xml * 実行前(修正前)。SSLのポートが「8443」 <?xml version='1.0' encoding='utf-8'?>  出力結果  コンソール画面 Current SSL Port : 8443 New SSL Port : 18443  conf/server.xml * 実行後(修正後)。SSLのポートが「18443」に書き換わっている <?xml version="1.0" encoding="utf-8" standalone="no"?>

 参考文献 http://www6.airnet.ne.jp/manyo/xml/dom_java/step23.html http://blog.goo.ne.jp/xmldtp/e/d7a9634935183c6a58745d4aedd8c775

 関連記事

XML API の使い分けについて
https://dk521123.hatenablog.com/entry/2015/06/06/100247
xPathXMLの参照・書き換え~
https://dk521123.hatenablog.com/entry/2014/07/03/000100