Hi all,
I'm trying to convert a .csv file to .xml file. I've written some codes but there is error.
The result in .xml file should display 2 different rows of data, but instead the program just took the last row of data to form the .xml file. Which means that the result displayed consists of 2 same row of data. So, how should I go about writing the codes in order to make the 2 different row of data be displayed? Any advice is greatly appreciated.
Below is my codings:
/*
* CSVToXML.java
*/
package com.dotcomerp.tools.classes;
import java.io.File;
import java.util.*;
import javax.swing.text.*;
import org.jdom.Element;
import com.dotcomerp.tools.classes.*;
import com.dotcomerp.tools.interfaces.*;
public class CSVToXML implements DataConvert {
private static final String sTagRow = "tag";
private static final String sDataRow = "data";
public CSVToXML()
{
}
/*
/**
* Convert input string format to output format
* @param sInput input string
* @return output string
*/
public String convert(String sInput){
Element eResult = new Element("result");
List lResult = eResult.getMixedContent();
List lTags = null;
List list = StringUtility.splitAll(sInput, "\r\n");
Iterator i = list.iterator();
boolean bFirstRow = true;
List lTagsArray = new ArrayList();
Element eRow = null;
//iterate for the row
while (i.hasNext()) {
String sRow = (String)i.next();
System.out.println("sRow: " + sRow);
//System.out.println("\n\neresult 1:" + XMLParser.elemToString(eResult));
if(isTagName(sRow)){
//retrieve tags information from the first few rows
lTags = StringUtility.splitAll(sRow, ",");
lTagsArray.add(lTags);
System.out.println("lTagsArray: " + lTagsArray);
}
else
{
if(bFirstRow)
{ //last tags row
lTags = StringUtility.splitAll(sRow, ",");
lTagsArray.add(lTags);
System.out.println("lTagsArray2: " + lTagsArray);
eRow = getTagElement(lTagsArray);
//System.out.println("eRow: " + XMLParser.elemToString(eRow));
bFirstRow = false;
}
else
{
List lData = StringUtility.splitAll(sRow, ",");
System.out.println("lData: " + lData);
//this statement is to iterate the data
Iterator iData = lData.iterator();
//eRow is the element containing tags, lParent == <f/> <d/>
List lParent = eRow.getChildren();
Iterator iParent = lParent.iterator();
System.out.println("\n\neresult 2:" + XMLParser.elemToString(eResult));
while(iParent.hasNext())
{
//get the parent tags, <f/> <d/>
Element eParent = (Element)iParent.next();
System.out.println("Parent: " + eParent);
//get the tags WITHIN the parent tags
List lChildren = eParent.getChildren();
System.out.println("lChildren: " + lChildren);
//this statement is to iterate the children tags
Iterator iChild = lChildren.iterator();
//List lPrevious =
System.out.println("\n\neresult 3:" + XMLParser.elemToString(eResult));
while(iChild.hasNext() && iData.hasNext())
{
Element eChildTag = (Element)iChild.next();
String sData = (String)iData.next();
Element ans = eChildTag.setText(sData);
System.out.println("eChildTag: " + eChildTag);
System.out.println("sData: " + sData);
System.out.println("-----------------------");
}
}
System.out.println("\n\neresult before:" + XMLParser.elemToString(eResult));
lResult.add(eRow);
System.out.println("\n\nerow:" + XMLParser.elemToString(eRow));
System.out.println("\n\neresult after:" + XMLParser.elemToString(eResult));
}
}
}
//System.out.println("\n\neresult final:" + XMLParser.elemToString(eResult));
return XMLParser.elemToString(eResult);
}
/**
* Convert file content from one form to another
* @param sInputFileName input file name
* @param sOutputFileName output file name
*/
public void convert(String sInputFileName,String sOutputFileName)
{
DataConvert dc = new CSVToXML();
System.out.println("\nFile Content Conversion");
try
{
//read content of file
String sInput = FileUtility.readFile(sInputFileName);
//convert file content to xml format
String sOutput = dc.convert(sInput);
//store the edited file content to the sOutputfileName
FileUtility.overwriteFile(sOutputFileName, sOutput);
}
catch(Exception e)
{
System.out.println("Errors : " + e.getMessage());
}
}
/**
* Check for tag name row.
* Case 1: ,....
* Case 2: ...,
* Case 3: ...,,....
*/
private boolean isTagName(String sRow)
{
if(sRow.charAt(0)==',' | | sRow.charAt(sRow.length()-1)== ',' | | sRow.indexOf(",,") >= 0) {
//get tag names after the "tag" tag
//String tag = StringUtility.getBefore(",", sRow);
return true;
}
return false;
}
private Element getTagElement(List lTagsArray)
{
Element eData = new Element("row");
Element eRoot = eData;
//lTagsArray is the list containing tags, this method iterates thru the tags
Iterator i = lTagsArray.iterator();
Element eTag = null;
//count the row
int iCount = 0;
while(i.hasNext())
{ //iterating a row at a time
List lRow = (List)i.next();
Iterator iRow = lRow.iterator();
while(iRow.hasNext())
{
//extract/iterate the tag names within the current row
String sTag = (String)iRow.next();
if(!"".equals(sTag))
{ //new element found
eTag = new Element(sTag);
eRoot.getMixedContent().add(eTag);
}
//iterating the lTagsArray to check whether if the next row exists
//the i here == lTagsArray
if(i.hasNext())
{
List lNextTagsRow = (List)lTagsArray.get(iCount+1);
//add child elements to eTag
Iterator iNextTag = lNextTagsRow.iterator();
if(iNextTag.hasNext())
{
String sNextTag = (String)iNextTag.next();
Element eNewTag = new Element(sNextTag);
eTag.getMixedContent().add(eNewTag);
lNextTagsRow.remove(sNextTag);
}
}
}
iCount++;
}
return eData;
}
}
------------------
0=),
Crystal