ページの先頭行へ戻る
Interstage Shunsaku Data Manager アプリケーション開発ガイド

J.1.3 特定のXML文書をすべて取得する

検索操作において、“J.1.1 検索条件に一致するXML文書の件数を求める”および“J.1.2 検索条件に一致するXML文書を指定した形式で取得する”といった操作を経て、絞り込んだ条件を基に、データすべてを取得することになります。


J.1.2 検索条件に一致するXML文書を指定した形式で取得する”の操作により得られたホテル名を基に、該当するホテルのデータをすべて取得する例を用いて、C APIの使用例を示します。


検索条件

「2006年7月18日に大阪で宿泊可能なホテルのうち、該当する30件分のホテル情報は?」

年月日(2006年7月18日)および、場所(大阪)を条件に検索を行います。また、ホテル名に対応するレコード識別子を用いて、ホテルの詳細情報を取得します。


APIの使用例


以下にC APIを使用したプログラミング例を示します。

#include <stdio.h>

#include "libshun.h"

/* 特定のXML文書をすべて取得する */
int main()
{
  /* ハンドル変数群 */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle_serch;
  SHUNHSTMT statementHandle_rec_id;

  /* 作業用変数群 */
  int status;
  int i;

  /* 入力パラメタ群 */
  char *hostName;
  int portNo;
  char *ShunFileName;
  int startNo;
  int returnRequestCount;
  char *queryForm;
  char *returnForm;
  char *sortForm;

  /* 出力パラメタ群 */
  int hitCount;
  int returnCount;
  int returnableCount;
  SHUNRECID *recID_search;
  SHUNRECID *recID_search_rec_id;
  SHUNDATA  *dataInfo_serch;
  SHUNDATA  *dataInfo_rec_id;
  SHUNPOS *firstPosition, *lastPosition;

  /* エラー用変数群 */
  SHUNHANDLE errorHandle;
  int errorLevel;
  char *errorMessage;


  /* 変数の初期化 */
  connectionHandle = NULL;
  statementHandle_serch = NULL;
  statementHandle_rec_id = NULL;

  /* コネクションハンドルの割当て */
  status = ShunAllocHandle( NULL, &connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* ホスト名・ポート番号、Shunsakuファイル名を指定してコネクションを確立 */
  hostName = "DirSvr1";
  portNo = 23101;
  ShunFileName = "shunsakuFile1";
  status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* データ操作ハンドルの割当て */
  status = ShunAllocHandle( connectionHandle, &statementHandle_serch );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }
  
  /* 検索件数、検索式などの指定*/
  startNo = 1;
  returnRequestCount = 30;
  queryForm = "/document {/base/prefecture == '大阪' "
              "AND /information/date == '2006年07月18日'}";
  returnForm = "/document/base/name, /document/base/price";
  sortForm = NULL;

  /* API呼出し データ検索 */
  status = ShunSearch(
    statementHandle_serch,
    startNo,
    NULL,
    0,
    returnRequestCount,
    queryForm,
    returnForm,
    sortForm,
    &hitCount,
    &returnCount,
    &returnableCount,
    &recID_search,
    &dataInfo_serch,
    &firstPosition,
    &lastPosition );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_serch;
    goto ErrorEnd;
  }

  /* ヒット件数の取出し */
  printf( "ヒット件数                = %d\n", hitCount );
  printf( "応答件数                  = %d\n", returnCount );

  
  /* データ操作ハンドルの割当て */
  status = ShunAllocHandle( connectionHandle, &statementHandle_rec_id );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }
  
  /* IDを基にデータすべてを取得 */
  if ( hitCount > 0 ) {
    status = ShunSearchRecId(
      statementHandle_rec_id,
      returnCount,
      recID_search,
      &recID_search_rec_id,
      &dataInfo_rec_id );
    if ( status != SHUN_SUCCESS ) {
      errorHandle = (SHUNHANDLE)statementHandle_rec_id;
      goto ErrorEnd;
    }
    if (recID_search_rec_id != NULL) {
      for (i = 0; i < returnCount; ++i) {
        if ( dataInfo_rec_id[i].Data != NULL ) {
          printf( "[詳細]%d件目 =%s\n", startNo + i, dataInfo_rec_id[i].Data);
        }
        else {
          printf( "[詳細]%d件目 =なし\n", startNo + i );
        }
      }
    }
  }
  
  /* データ操作ハンドルの解放 */
  status = ShunFreeHandle( statementHandle_rec_id );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_rec_id;
    goto ErrorEnd;
  }
  
  /* データ操作ハンドルの解放 */
  status = ShunFreeHandle( statementHandle_serch );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_serch;
    goto ErrorEnd;
  }

  /* コネクションの切断 */
  status = ShunDisconnect( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* コネクションハンドルの解放 */
  status = ShunFreeHandle( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  return 0;



ErrorEnd: /* エラーが発生した際のエラー処理 */

  /* エラー情報の取得 */
  status = ShunGetErrorMessage(errorHandle,
                               &errorLevel, &errorMessage);

  if ( status == SHUN_SUCCESS ) {
    switch ( errorLevel ) {
    case SHUN_ERROR_CONNECTION:
      printf("エラーレベル              = SHUN_ERROR_CONNECTION\n");
      break;
    case SHUN_ERROR_DATA:
      printf("エラーレベル              = SHUN_ERROR_DATA\n");
      break;
    }
    printf("エラーメッセージ          = %s\n", errorMessage);
  }
  else {
    printf("エラーメッセージ取得エラー : %d\n", status);
  }

  /* データ操作ハンドルの解放 */
  if ( statementHandle_rec_id != NULL ) {
    status = ShunFreeHandle( statementHandle_rec_id );
    if ( status != SHUN_SUCCESS ) {
      printf("データ操作ハンドル解放エラー : %d\n", status);
    }
  }

  /* データ操作ハンドルの解放 */
  if ( statementHandle_serch != NULL ) {
    status = ShunFreeHandle( statementHandle_serch );
    if ( status != SHUN_SUCCESS ) {
      printf("データ操作ハンドル解放エラー : %d\n", status);
    }
  }

  /* コネクションの切断 */
  if ( connectionHandle != NULL) {
    status = ShunDisconnect( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("コネクション切断エラー : %d\n", status);
     }
  }

  /* コネクションハンドルの解放 */
  if ( connectionHandle != NULL) {
    status = ShunFreeHandle( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("コネクションハンドル解放エラー : %d\n", status);
    }
  }

  return 1;
}

実行結果

ヒット件数                = 3
応答件数                  = 3
 [詳細]1件目 = <document>
    <base>
        <name>ホテル1</name>
        <prefecture>大阪</prefecture>
        <address>大阪府大阪市中央区</address>
        <detail>http://xxxxx.co.jp</detail>
        <price>9000</price>
    </base>
    <information>
        <date>2006年07月18日</date>
    </information>
    <note>バス付 トイレ付 地下鉄 △△駅徒歩02分</note>
</document>
[詳細]2件目 = <document>
    <base>
        <name>ホテル2</name>
        <prefecture>大阪</prefecture>
        <address>大阪府大阪市中央区</address>
        <detail>http://xxxxx.co.jp</detail>
        <price>6000</price>
    </base>
    <information>
        <date>2006年07月18日</date>
    </information>
    <note>バス付 トイレ付 地下鉄 △△駅徒歩05分</note>
</document>
[詳細]3件目 = <document>
    <base>
        <name>ホテル3</name>
        <prefecture>大阪</prefecture>
        <address>大阪府大阪市中央区</address>
        <detail>http://xxxxx.co.jp</detail>
        <price>7500</price>
    </base>
    <information>
        <date>2006年07月18日</date>
    </information>
    <note>バス付 トイレ付 地下鉄 △△駅徒歩10分</note>
</document>