データベースまたは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; } |
データベースまたは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 { : } |