AnyData::Format::XMLモジュール (AnyData Ver. 0.05) (日本語チョウ訳)

by Hippo2000(2001/8/5)

日本語チョウ訳シリーズ AnyData::Format::XMLモジュールなのです。

いつものことですが、わかりにくい(あやしい)説明は本物を見てください。(^^;

原本の著作権はJeff Zucker氏がお持ちです(詳しくは著作権情報を見てください)。Jeff Zucker氏にはメールで了解をいただきました。

内容等が間違っていたら修正します。ご連絡ください。


目次


名前

 AnyData::Format::XML - XMLへのtieされたハッシュとDBIによるアクセス

概要

 # 多次元のtieされたハッシュを介してXMLデータにアクセス
 # 完全な詳細はAnyData.podをご覧ください
 #
 use AnyData;
 my $table = adTie( 'XML', $file, $mode, $flags );

 または
 # データをXMLから、そしてXMLへ変換する
 # 完全な詳細はAnyData.podをご覧ください
 #
 use AnyData;
 adConvert( 'XML', $file1, $any_other_format, $file2, $flags );
 adConvert( $any_other_format, $file1, 'XML', $file2, $flags );
 または
 # DBIとSQLを介してアクセス
 # 完全な詳細はDBD::AnyData.podをご覧ください
 #
 use DBI;
 my $dbh = DBI->connect( 'dbi:AnyData' );
 $dbh->func('mytable','XML',$file,$flags,'ad_catalog');

こられの例のすべてに適合するオプションのフラグについての下記の説明をご覧ください。


説明

This module allows you to create, search, modify and/or convert XML data and files by treating them as databases without having to actually create separate database files. The data can be accessed via a multi-dimensional tiedhash using AnyData.pm or via DBI and SQL commands using DBD::AnyData.pm. See those modules for complete details of usage.

The module is built on top of Michel Rodriguez's excellent XML::Twig which means that the AnyData interfaces can now include information from DTDs, be smarter about inferring data structure, reduce memory consumption on huge files, and provide access to many powerful features of XML::Twig and XML::Parser on which it is based.

Importing options allow you to import/access/modify XML of almost any length or complexity. This includes the ability to access different subtrees as separate or joined databases.

Exporting and converting options allow you to take data from almost any source (a perl array, any DBI database, etc.) and output it as an XML file. You can control the formating of the resulting XML either by supplying a DTD listing things like nesting of tags and which columns should be output as attributes and/or you can use XML::Twig pretty_print settings to generate half a dozen different levels of compactness or whitespace in how the XML looks.

The documentaion below outlines the special flags that can be used in either of the interfaces to fine-tune how the XML is treated.

The flags listed below define the relationship between tags and attributes in the XML document and columns in the resulting database. In many cases, you can simply accept the defaults and the database will be built automatically. However, you can also fine tune the generation of the database by specifying which tags and attributes you are interested in and their relationship with database columns.


使用法

必要なもの

tieされたハッシュ・インタフェースを使うためには以下のものが必要です:

 AnyData
 XML::Twig
 XML::Parser

DBI/SQLインタフェースを使うためには、上記のほかに以下のものも必要です

 DBI
 DBD::AnyData

必要なフラグ(なし)

If no flags are specified, then the module determines the database structure from examining the file or data itself, making use of the DTD if there is one, otherwise scanning the first child of the XML tree for structural information.

オプションのフラグ

デフォルトの動きで十分でなければ、カラム名を定義するために使われる"レコード・タグ(record tag)"を指定するか、完全なタグ-カラム マッピングを定義することができます。

簡単なXMLでは、何もフラグは必要ありません:

 <table>
    <row row_id="1"><name>Joe</name><location>Seattle</location></row>
    <row row_id="2"><name>Sue</name><location>Portland</location></row>
 </table>

レコード・タグはデフォルトでは"row"という最初の子供になります。カラム名はレコードタグの属性とレコード・タグの下に入っているすべてのタグから生成されます。そのためこの例でのカラム命は"row_id"、"name","location"になります。

もしレコード・タグが最初の子供でなければ、指定する必要があります。例えば:

 <db>
   <table table_id="1">
     <row row_id="1"><name>Joe</name><location>Seattle</location></row>
     <row row_id="2"><name>Sue</name><location>Portland</location></row>
   </table>
   <table table_id="2">
     <row row_id="1"><name>Bob</name><location>Boise</location></row>
     <row row_id="2"><name>Bev</name><location>Billings</location></row>
   </table>
 </db>

この場合、それがツリーの最初の子供ではないので、"row"をレコード・タグとして指定する必要があります。カラム名はrowの親の属性、rowの属性、サブのタグからから作られます。つまり"table_id"、"row_id"、"name"、"location"になります。

場合によっては特定の完全なタグ−カラムのマッピングを指定する必要があります。例えばXMLで使われているものとは違う名前をデータベース・カラムに使いたい(特にXMLタグが適切なSQLカラム命でない場合)など。XMLツリー内で異なる場所で同じ名前を持つ2つのタグがある場合にも、マッピングを指定する必要があります。

カラム・マッピングはカラム定義の配列へのリファレンスです。カラム定義は簡単なタグの名前か、XMLタグの完全なパスが入ったキー、値には希望するカラム名のエイリアスが入ったハッシュへのリファレンスです。

例えば:

  col_map => [ 'part_id', 'part_name', 'availability' ];

これはこれらの名前をもつ最初の3つのタグを見つけ、タグと同じ名前を使ってデータベースを作ります

あついは:

  col_map => [
               { '/parts/shop/id'        => 'shop_id'},
               { '/parts/shop/part/id'   => 'part_id'},
               { '/parts/shop/part/name' => 'part_name'},
             ];

これは左で参照される3つのタグを見つけ、右で参照されるこれら3つのカラム名を持ったデータベースを作成します。

XMLに出力するとき、出力を制御するためにDTDを指定することができます。例えば、CSVまたは配列(Array)からテーブルをインポートしたとき、XMLとして出力することができ、そのカラムがタグになり、どれが属性となるかを、タグのネストもDTDで指定することができます。

XMLフォーマット・パーサーは、それ自身はXML::Parserをベースとしている、Michel Rodriguez氏のXML::Twigの上に構築されています。これらのモジュールのパラメータは、adTie()や他のコマンドのためのフラグで渡すことができます。これにはXMLをどのように出力するかをしていするための"prettyPrint"フラグも含まれProtocolEncodingのように、ProtocolEncodingのデフォルトは'ISO-8859-1'、他のすべてのフラグはXML::TwigとXML::Parserのデフォルトが保たれています。詳細についてはそれらのモジュールのドキュメントをご覧ください。

注意:他のフォーマットとは違い、XMLフォーマットは変更をそれが入った時点では保存せず、adExport()コマンドで明示的に保存するよう要求したときにのみ変更は保存されます。


作者と著作権(AUTHOR & COPYRIGHT)

copyright 2000, Jeff Zucker <jeff@vpservices.com> all rights reserved

 


ご意見、ご質問はこちらの掲示板で受け付けています。
またメールは河馬屋(Nifty)にお願いします。