ここ1週間ほどゴリゴリソース書いてたので公開。
Excelデータ(CSVで保存) をXML形式に変換して出力するプログラム。
入出力のデータはソースの中で呼び出してます。
あと、Winでの使用を前提としていますので文字コードは全てShift_Jisでやってます。
UTF-8で出力したい場合はXMLヘッダ部分と文字コードの変換機能を実装する必要があります。
誰得ソースだけど好きに使っていいのよ。
といってもCSV読み込みロジック借り物だけど。
XML出力ロジックとか思いついたの適当に入れてるのでもっとスマートな方法あるよとかあればご連絡ください。
以下ソース全量
/* csv.java */
/**
* JavaでCSVファイルから文字を読み込み、XML形式に変換してファイルに書き出すプログラム。
* 最初に実行するプログラム
*
* 2011/01/17
* Erlkonig
*/
import java.io.*;
import java.util.*;
public class csv {
public String inFile = null;
private BufferedReader bufferedreader = null;
private List<String> list = new ArrayList<String>();
public csv() {
}
/**
* 入力バッファーを生成し、中身を書き出す。
* @param inFile CSVファイル名
* @throws IOException
*/
public csv(String inFile) throws IOException { // 指定したファイルがあるかチェック
this.inFile = inFile;
bufferedreader = new BufferedReader(new FileReader(inFile)); // BufferedReaderオブジェクトbufferedreaderを生成;FileReaderオブジェクトinFileを生成
String stemp;
while((stemp = bufferedreader.readLine()) != null){ // 読み込みデータが無くなるまで読み込み
list.add(stemp);
}
}
/**
* CSV内容格納リストを取得します。
* @param CSV内容リスト
* @throws IOException
*/
public List<String> getList() throws IOException {
return list;
}
/**
* CSVファイルの行数を取得します。
* @return CSV行数
*/
public int getRowNum() {
return list.size();
}
/**
* CSVファイルの列数を取得します。
* @return CSV列数
*/
public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {
return list.get(0).toString().split(",").length;
}else if (list.get(0).toString().trim().length() != 0) {
return 1;
}else{
return 0;
}
}else{
return 0;
}
}
/**
* 指定されたインデックス内容を取得します(行)。
*
*/
public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index).toString().trim();
else
return null;
}
/**
* 開いて入力ファイルをクローズします。
* @throws IOException
*/
public void FileClose()throws IOException {
this.bufferedreader.close();
}
//ファイルをXML形式に変換
/**
* XMLの内容をバッファに書き込む。
* 入力したデータをXML形式に変換して出力します。
* 中身はゴリゴリ文字列操作。
*/
/**
* ヘッダーの作成
* @return ヘッダー
*/
public String header(){
String header = ("<?xml version=\"1.0\" encoding=\"Shift_JIS\" standalone=\"no\"?>");
return header;
}
/**
* root要素として「データラベル」要素を生成
* @return データラベル
*/
public String root(){
String root = ("データラベル"); // データラベルノード作成
return root;
}
/**
* サークルデータ要素を生成し、イベント名属性を追加
* @return サークルデータ
*/
public String volume(){
String document = "サークルデータ";
String event = "イベント名";
String named = "東方おかやま桃太郎まつり";
String volume = (document + " " + event + "=" + "\"" + named + "\"");
return volume;
}
/**
* メインルーチン。
* ファイル作成用コード
* @throws IOException
*/
public void editfile() throws IOException{
csv cu = new csv("D:/Java/CSV2XML/testdata/circledata.csv"); // アドレス直打ちしてます。
List<String> tt = cu.getList();
for (Iterator<String> itt = tt.iterator();itt.hasNext();){ // CSVの内容をコンソールに出力
System.out.println(itt.next().toString());
}
System.out.println("\n");
FileWriter fw = new FileWriter("D:/Java/CSV2XML/testdata/circledata.xml");
BufferedWriter bw = new BufferedWriter(fw); //BufferedWriterオブジェクトbufferedwriterを生成;FileWriterオブジェクトoutFileを生成
bw.write(header());
String roottag = ("<" + root() + ">");
bw.write(roottag);
//ラベルのデータをセットしていく}
String str1 = cu.getRow(0);
String [] str2 = str1.split(",", 0);
//データの中身をセットしていく
for (int i = 0; i < cu.getRowNum()-1; i++){
try{
String volumetag = ("<" + volume() + ">");
bw.write(volumetag);
int j = i + 1;
String dat1 = cu.getRow(j);
String [] dat2 = dat1.split(",",0);
//ラベルとデータ合成
for (int n = 0; n < cu.getColNum(); n++){
String str3 = str2[n].replaceAll("\"","");
String label = ("<_" + str3 + ">");
String labelend = ("</_" + str3 + ">");
String data = dat2[n].replaceAll("\"","");
String outdata = (label + data + labelend);
bw.write(outdata);
}
int doclen = volume().indexOf(" ");
String volstr = volume().substring(0,doclen);
String volumeend = ("</" + volstr + ">");
bw.write(volumeend);
} catch (IndexOutOfBoundsException e) {
}
}
String rootend = ("</" + root() + ">");
bw.write(rootend);
cu.FileClose();
bw.close();
}
/**
* メイン
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
csv editfile = new csv();
editfile.editfile();
}
}
Excelデータ(CSVで保存) をXML形式に変換して出力するプログラム。
入出力のデータはソースの中で呼び出してます。
あと、Winでの使用を前提としていますので文字コードは全てShift_Jisでやってます。
UTF-8で出力したい場合はXMLヘッダ部分と文字コードの変換機能を実装する必要があります。
誰得ソースだけど好きに使っていいのよ。
といってもCSV読み込みロジック借り物だけど。
XML出力ロジックとか思いついたの適当に入れてるのでもっとスマートな方法あるよとかあればご連絡ください。
以下ソース全量
/* csv.java */
/**
* JavaでCSVファイルから文字を読み込み、XML形式に変換してファイルに書き出すプログラム。
* 最初に実行するプログラム
*
* 2011/01/17
* Erlkonig
*/
import java.io.*;
import java.util.*;
public class csv {
public String inFile = null;
private BufferedReader bufferedreader = null;
private List<String> list = new ArrayList<String>();
public csv() {
}
/**
* 入力バッファーを生成し、中身を書き出す。
* @param inFile CSVファイル名
* @throws IOException
*/
public csv(String inFile) throws IOException { // 指定したファイルがあるかチェック
this.inFile = inFile;
bufferedreader = new BufferedReader(new FileReader(inFile)); // BufferedReaderオブジェクトbufferedreaderを生成;FileReaderオブジェクトinFileを生成
String stemp;
while((stemp = bufferedreader.readLine()) != null){ // 読み込みデータが無くなるまで読み込み
list.add(stemp);
}
}
/**
* CSV内容格納リストを取得します。
* @param CSV内容リスト
* @throws IOException
*/
public List<String> getList() throws IOException {
return list;
}
/**
* CSVファイルの行数を取得します。
* @return CSV行数
*/
public int getRowNum() {
return list.size();
}
/**
* CSVファイルの列数を取得します。
* @return CSV列数
*/
public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {
return list.get(0).toString().split(",").length;
}else if (list.get(0).toString().trim().length() != 0) {
return 1;
}else{
return 0;
}
}else{
return 0;
}
}
/**
* 指定されたインデックス内容を取得します(行)。
*
*/
public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index).toString().trim();
else
return null;
}
/**
* 開いて入力ファイルをクローズします。
* @throws IOException
*/
public void FileClose()throws IOException {
this.bufferedreader.close();
}
//ファイルをXML形式に変換
/**
* XMLの内容をバッファに書き込む。
* 入力したデータをXML形式に変換して出力します。
* 中身はゴリゴリ文字列操作。
*/
/**
* ヘッダーの作成
* @return ヘッダー
*/
public String header(){
String header = ("<?xml version=\"1.0\" encoding=\"Shift_JIS\" standalone=\"no\"?>");
return header;
}
/**
* root要素として「データラベル」要素を生成
* @return データラベル
*/
public String root(){
String root = ("データラベル"); // データラベルノード作成
return root;
}
/**
* サークルデータ要素を生成し、イベント名属性を追加
* @return サークルデータ
*/
public String volume(){
String document = "サークルデータ";
String event = "イベント名";
String named = "東方おかやま桃太郎まつり";
String volume = (document + " " + event + "=" + "\"" + named + "\"");
return volume;
}
/**
* メインルーチン。
* ファイル作成用コード
* @throws IOException
*/
public void editfile() throws IOException{
csv cu = new csv("D:/Java/CSV2XML/testdata/circledata.csv"); // アドレス直打ちしてます。
List<String> tt = cu.getList();
for (Iterator<String> itt = tt.iterator();itt.hasNext();){ // CSVの内容をコンソールに出力
System.out.println(itt.next().toString());
}
System.out.println("\n");
FileWriter fw = new FileWriter("D:/Java/CSV2XML/testdata/circledata.xml");
BufferedWriter bw = new BufferedWriter(fw); //BufferedWriterオブジェクトbufferedwriterを生成;FileWriterオブジェクトoutFileを生成
bw.write(header());
String roottag = ("<" + root() + ">");
bw.write(roottag);
//ラベルのデータをセットしていく}
String str1 = cu.getRow(0);
String [] str2 = str1.split(",", 0);
//データの中身をセットしていく
for (int i = 0; i < cu.getRowNum()-1; i++){
try{
String volumetag = ("<" + volume() + ">");
bw.write(volumetag);
int j = i + 1;
String dat1 = cu.getRow(j);
String [] dat2 = dat1.split(",",0);
//ラベルとデータ合成
for (int n = 0; n < cu.getColNum(); n++){
String str3 = str2[n].replaceAll("\"","");
String label = ("<_" + str3 + ">");
String labelend = ("</_" + str3 + ">");
String data = dat2[n].replaceAll("\"","");
String outdata = (label + data + labelend);
bw.write(outdata);
}
int doclen = volume().indexOf(" ");
String volstr = volume().substring(0,doclen);
String volumeend = ("</" + volstr + ">");
bw.write(volumeend);
} catch (IndexOutOfBoundsException e) {
}
}
String rootend = ("</" + root() + ">");
bw.write(rootend);
cu.FileClose();
bw.close();
}
/**
* メイン
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
csv editfile = new csv();
editfile.editfile();
}
}
0 件のコメント:
コメントを投稿