Interstage Shunsaku Data Manager アプリケーション開発ガイド - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX共通 -
|
目次
索引

|
12.6.3 XMLGeneratorの生成からXML文書の取得
XMLGeneratorの生成からXML文書の取得までのAPIの使用例を、以下に示します。
データベースまたはCSVファイルに格納されているデータから取得したResultSetオブジェクトを基に、XMLGeneratorによる変換を行ったあと、XML文書をStringにより取り出し、出力する例を以下に示します。
XMLGenerator gen = null;
GeneratorMap map = null;
DocumentFountain df = null;
try
{
gen = new XMLGenerator();
map = new GeneratorMap("map01.xml");
df = gen.createDocumentFountain( DocumentFountain.TYPE_STRING ,rs, map );
df.setEncoding("SJIS");
}
catch(Exception e)
{
e.printStackTrace();
return;
}
String ResultStr = null;
try
{
while(df.hasXMLDocument())
{
ResultStr = (String)df.getXMLDocument();
System.out.println("Result:"+ResultStr);
}
rs.close(); // 入力がデータベースの場合は、Connection切断も必要
}
catch(Exception e)
{
e.printStackTrace();
return;
}
}
}
|
データベースまたはCSVファイルに格納されているデータから取得したResultSetオブジェクトを基に、XMLGeneratorによる変換を行ったあと、XML文書をStreamにより取り出し、出力する例を以下に示します。
import java.io.File;
import java.io.FileOutputStream;
:
XMLGenerator gen = null;
GeneratorMap map = null;
DocumentFountain df = null;
try
{
gen = new XMLGenerator();
map = new GeneratorMap("map01.xml");
df = gen.createDocumentFountain( DocumentFountain.TYPE_STREAM, rs, map );
df.setEncoding("SJIS");
}
catch(Exception e)
{
e.printStackTrace();
return;
}
FileOutputStream ResultStream = null;
try
{
ResultStream = new FileOutputStream(new File("filename"));
df.setDestination(ResultStream);
while(df.hasXMLDocument())
{
df.getXMLDocument();
}
ResultStream.close();
rs.close(); // 入力がデータベースの場合は、Connection切断も必要
}
catch(Exception e)
{
e.printStackTrace();
return;
}
}
} |
データベースまたはCSVファイルに格納されているデータから取得したResultSetオブジェクトを基に、XMLGeneratorによる変換を行ったあと、XML文書をファイル出力する例を以下に示します。
ファイルの場合、1レコード1ファイルに出力します。そのため、出力先のファイル名を順次変更する必要があります。
XMLGenerator gen = null;
GeneratorMap map = null;
DocumentFountain df = null;
try
{
gen = new XMLGenerator();
map = new GeneratorMap("map01.xml");
df = gen.createDocumentFountain( DocumentFountain.TYPE_FILE, rs, map );
df.setEncoding("SJIS");
}
catch(Exception e)
{
e.printStackTrace();
return;
}
try
{
int i = 1;
while(df.hasXMLDocument())
{
df.setDestination("result" + i + ".xml");
df.getXMLDocument();
i++;
}
rs.close(); // 入力がデータベースの場合は、Connection切断も必要
}
catch(Exception e)
{
e.printStackTrace();
return;
}
}
} |
データベースまたはCSVファイルに格納されているデータから取得したResultSetオブジェクトを基に、XMLGeneratorによる変換を行ったあと、XML文書をDOMツリーとして取り出し、アプリケーションで利用する例を以下に示します。
import org.w3c.dom.Document;
:
XMLGenerator gen = null;
GeneratorMap map = null;
DocumentFountain df = null;
try
{
gen = new XMLGenerator();
map = new GeneratorMap("map01.xml");
df = gen.createDocumentFountain( DocumentFountain.TYPE_DOM, rs, map );
df.setEncoding("SJIS");
}
catch(Exception e)
{
e.printStackTrace();
return;
}
Document docRoot = null;
try
{
while(df.hasXMLDocument())
{
docRoot = (Document)df.getXMLDocument();
// ここでは、DOMツリーとして受信したdocRootを利用する
// アプリケーション処理を記載する。
}
rs.close(); // 入力がデータベースの場合は、Connection切断も必要
}
catch(Exception e)
{
e.printStackTrace();
return;
}
}
}
|
データベースまたはCSVファイルに格納されているデータから取得したResultSetオブジェクトを基に、XMLGeneratorによる変換を行ったあと、XML文書をSAXとして取り出し、アプリケーションで利用する例を以下に示します。
なお、setDestination()メソッドの引数には、org.xml.sax.ContentHandlerインタフェースを実装したクラスのインスタンスを設定してください。
以下の例では、SAXhandlerクラスはorg.xml.sax.ContentHandlerインタフェースを別途実装しているものとします。
import org.xml.sax.helpers.DefaultHandler;
:
XMLGenerator gen = null;
GeneratorMap map = null;
DocumentFountain df = null;
try
{
gen = new XMLGenerator();
map = new GeneratorMap("map01.xml");
df = gen.createDocumentFountain( DocumentFountain.TYPE_SAX,rs, map );
df.setEncoding("SJIS");
}
catch(Exception e)
{
e.printStackTrace();
return;
}
try
{
while(df.hasXMLDocument())
{
// ここでは、SAXイベントとして受信した
// アプリケーション処理を記載する。
SAXhandler sh = new SAXhandler();
df.setDestination(sh);
df.getXMLDocument();
}
rs.close(); // 入力がデータベースの場合は、Connection切断も必要
}
catch(Exception e)
{
e.printStackTrace();
return;
}
}
}
class SAXhandler extends DefaultHandler
{
:
}
|
All Rights Reserved, Copyright(C) 富士通株式会社 2006