CB3 - チュートリアル 第3章 - 3.10XMLファイルアクセスその2

 3.10.1概要

  • このサンプルの目的
     このアプリケーション作成例を通して、以下について知ることができます。
    • 名前空間を使用したXMLファイル(ライタ、リーダ)アクセッサの使用方法

  • サンプルアプリケーションの内容
     XMLファイルへの書込み、読込みを行い読み込み結果を標準出力へ表示するバッチアプリケーションです。  
     サンプルアプリケーションの概要を下図に示します。
     
     
     名前空間を使用したXMLファイルを扱う場合は、通常のXMLファイルのときと同様にXMLファイルアクセッサを使用します。
     XMLファイルに書込むときには、あらかじめ名前空間URIと名前空間の接頭辞を定義し、要素を出力するときに名前空間URIを指定して出力します。 読込むときは、名前空間を指定して要素を抽出します。
     
     このサンプルでは以下のファイルについて説明します。  
    • Namespace.java
       サービスのプログラムです。上図内の赤枠の部分に相当します。
    • writerConfig.xml
       XMLファイルライタ設定ファイルです。上図内のXMLファイルライタ設定ファイルに相当します。
    • readerConfig.xml
       XMLファイルリーダ設定ファイルです。上図内のXMLファイルリーダ設定ファイルに相当します。
    • applicationContext.xml
       CB3がserviceContext.xmlなどのspringフレームワーク関連の設定ファイルを読み込むための起点となる コンテキストファイルです。
      このファイルは内容が「3.5固定長ファイルアクセス - その1」と同じため省略します。
    • serviceContext.xml
       実行対象のサービスを定義します。
    • 起動用バッチファイル - xmlaccess.bat
       サービスを起動するためのバッチファイルです。
    • namespaceTestXML.xml
       書込み読込み対象のファイルです。

 3.10.2サービスの作成

  • Namespace.java

     01:public class Namespace extends CB3Service {
     02:
     03:    @Override
     04:    protected int doService(ServiceParameters serviceparameters,
     05:            RuntimeParameters runtimeparameters) throws ServiceException {
     06:        fileWrite();
     07:        fileRead();
     08:        return 0;
     09:    }
     10:
     11:    /**
     12:     * XMLファイルへの書き込みを行う
     13:     */
     14:    private void fileWrite(){
     15:
     16:        XMLFileWriter writer = (XMLFileWriter)getDataAccessContext().lookup("writerConfig");
     17:        writer.open();
     18:
     19:        XMLElement element = new XMLElement("cb3");
     20:        //名前空間URIを指定する
     21:        element.setURI("http://www.cybec.co.jp/cb3");
     22:        element.setString("welcome");
     23:
     24:        //名前空間URIと名前空間の接頭辞を指定する
     25:        writer.addNamespace("http://www.cybec.co.jp/cb3","cybec");
     26:        writer.dataElement(element);
     27:        writer.close();
     28:    }
     29:
     30:    /**
     31:     * ファイルの読み込みを行う
     32:     */
     33:    private void fileRead(){
     34:        XMLFileReader reader = (XMLFileReader)getDataAccessContext().lookup("readerConfig");
     35:        reader.open();
     36:
     37:        //XQueryを使い、cybec:cb3タグを読み込む
     38:        XMLPath path = reader.selectPath(
     39:                "declare namespace cybec = 'http://www.cybec.co.jp/cb3'$this//cybec:cb3");
     40:
     41:        //タグ内の要素名、要素を取り出す
     42:        XMLElement cb3 = path.next();
     43:        System.out.println("要素名 = [" + cb3.getName() + "],  要素 = [" + cb3.getString() + "]");
     44:        reader.close();
     45:    }
     46:
     47:}
    

    このサンプルはファイルへの書込み処理(14行目:fileWrite())を行った後、書込んだファイルの読込み(33行目:fileRead())処理を行っています。

    以下のXMLの書込みと読込みを行います。
    <cybec:cb3 xmlns:cybec="http://www.cybec.co.jp/cb3">welcome</cybec:cb3>
    

    ■書込み処理
    21行目:
    「cb3」要素のXMLElementに名前空間URIを指定します。

    25行目:
    XMLファイルライタに名前空間URIと接頭辞を追加します。

    ■読込み処理
    38〜39行目:
    「cybec:cb3」要素を検索します。名前空間を使用した要素を検索する場合は「declare namespace」で名前空間を定義します。

 3.10.3設定ファイルの作成

  • writerConfig.xml
    XMLファイルライタの設定ファイルです。

     01:<?xml version="1.0" encoding="UTF-8"?>
     02:<xml-writer
     03:  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     04:  xsi:noNamespaceSchemaLocation="XMLFileWriterConfig.xsd">
     05:  <format>
     06:    <filepath>namespaceTestXML.xml</filepath>
     07:  </format>
     08:</xml-writer>
    

    6行目:
    書込みファイルパスの設定を行います。

  • readerConfig.xml
    XMLファイルリーダの設定ファイルです。

     01:<?xml version="1.0" encoding="UTF-8"?>
     02:<xml-reader
     03:  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     04:  xsi:noNamespaceSchemaLocation="XMLFileReaderConfig.xsd">
     05:  <format>
     06:    <filepath>namespaceTestXML.xml</filepath>
     07:  </format>
     08:</xml-reader>
    

    6行目:
    読込みファイルパスの設定を行います。

  • serviceContext.xml
    このサンプルの実行に必要となるサービスのコンテキストファイルです。

     01:<?xml version="1.0" encoding="UTF-8"?>
     02:    <-- 途中省略 -->
     03:
     04:    <!-- ServiceProvider -->
     05:    <bean id="namespace" class="jp.co.cybec.cb3.container.provider.ServiceProviderImpl">
     06:        <property name="service"><ref bean="sample6"/></property>
     07:    </bean>
     08:    <!-- Service -->
     09:    <bean id="sample6" class="jp.co.cybec.cb3.sample.xml.namespace.Namespace">
     10:        <property name="dataAccessContext"><ref bean="accessContext"/></property>
     11:    </bean>
     12:    <!-- DataAccessContext -->
     13:    <bean id="accessContext" class="jp.co.cybec.cb3.accessor.DataAccessContextImpl">
     14:        <constructor-arg index="0"><ref bean="testDataAccessors"/></constructor-arg>
     15:    </bean>
     16:    <util:map id="testDataAccessors">
     17:        <entry key="readerConfig"><ref bean="xmlFileReader"/></entry>
     18:        <entry key="writerConfig"><ref bean="xmlFileWriter"/></entry>
     19:    </util:map>
     20:
     21:</beans>
    

    17行目〜18行目:
    XMLファイルアクセッサの設定を行っています。このサンプルでは書込み処理と読込み処理を行っているので XMLファイルリーダとXMLファイルライタの二つの設定を行っています。

 3.10.4サービスの実行

  • namespace.bat
     01:@echo off
     02:
     03:setlocal
     04:
     05:call ./set_classpath_com.bat
     06:
     07:set CLASSPATH=%CB3_HOME%/resources/config/namespace;%CLASSPATH%
     08:
     09:java jp.co.cybec.cb3.container.provider.ServiceProviderImpl -ServiceName namespace
     10:
     11:pause
    

    上記バッチファイルを実行すると下記のように表示されます。

    要素名 = [cb3],  要素 = [welcome]
    続行するには何かキーを押してください . . .
    

    namespaceTestXML.xml確認すると以下のようにデータが登録されています。