Help append Child problem
HELLO
i m converting csvfile to xml. using org andcom.Ostermiller.util.CSVParser i got out is like that.. but </RECORD> element not append properly
,when end of line only that childis append
help me
<CSV2XML>
<RECORD>
<Ticketid>DC2000000708378</Ticketid>
<Status>ss</Status>
<Severity>Relevant</Severity>
<Symptom>DDOS Stacheldraht client check gag</Symptom>
<Sentryid>trii-001</Sentryid>
<GatewayMessages>2</GatewayMessages>
<Ticketid>DC2000000708383</Ticketid>
<Status>Resolved</Status>
<Severity>Relevant</Severity>
<Symptom>named-probe-authors: Snort IDS480</Symptom>
<Sentryid>trii-001</Sentryid>
<GatewayMessages>3</GatewayMessages>
<Ticketid>DC2000000708384</Ticketid>
<Status>Resolved</Status>
<Severity>Relevant</Severity>
<Symptom>dns_named-probe-version: Snort</Symptom>
<Sentryid>trii-001</Sentryid>
<GatewayMessages>2</GatewayMessages>
</RECORD>
</CSV2XML>
my java file
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import com.Ostermiller.util.CSVParser;
public class CSVReader {
// Protected Properties
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;
protected String StrValues;
protected String szNewString;
public CSVReader()
{
try
{
domFactory = DocumentBuilderFactory.newInstance();
domBuilder = domFactory.newDocumentBuilder();
}
catch(FactoryConfigurationError exp)
{
System.err.println(exp.toString());
}
catch(ParserConfigurationException exp)
{
System.err.println(exp.toString());
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
}
//
public int convertFile(String csvFileName, String xmlFileName)
{
int rowsCount = -1;
try
{
Document newDoc = domBuilder.newDocument();
// Root element
Element rootElement = newDoc.createElement("CSV2XML");
newDoc.appendChild(rootElement);
FileReader fReader = new FileReader(csvFileName);
CSVParser Strbuffer = new CSVParser(fReader);
Strbuffer.setCommentStart("#;!");
Strbuffer.setEscapes("nrtf", "\n\r\t\f");
String[] strValues = null;
String[] strTitle =null;
// Assumption: first line in CSV file is column/field names
// As the column names are used to name the elements in the XML file,
// avoid using any other characters not suitable for XML element naming
if ((strTitle =Strbuffer.getLine()) != null)
{
Element rowElement = newDoc.createElement("RECORD");
// Now we know the columns, now read data row lines
while((strValues = Strbuffer.getLine()) != null)
{
System.out.println(strValues.length);
for(int i=0;i<strTitle.length;i++)
{
String Header = strTitle[i];
String Value = strValues[i];
try
{
Element curElement = newDoc.createElement(Header);
curElement.appendChild(newDoc.createTextNode(Value ));
rowElement.appendChild(curElement);
// System.out.println("***** "+"**"+Header+"**"+rowElement.appendChild(curEleme nt).getNodeName());
}
catch(Exception exp)
{
System.out.println(exp +"4");
}
}
// System.out.println("***** **"+rootElement.appendChild(rowElement).getNodeVal ue());
rootElement.appendChild(rowElement);
rowsCount++;
}
}
Strbuffer.close();
fReader.close();
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(newDoc);
Result dest = new StreamResult(new File(xmlFileName));
aTransformer.transform(src, dest);
rowsCount++;
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
return rowsCount;
}
public static void main(String args[])
{
try
{
if(args.length != 2)
{
System.out.println("Usage: java CSV2XML <inputCSVFile> <outputXMLFile>");
return;
}
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
try
{
CSVReader csvConverter = new CSVReader();
int rowsCount = csvConverter.convertFile(args[0], args[1]);
if(rowsCount >= 0)
{
System.out.println("CSV File '" + args[0] +
"' successfully converted to XML File '"+ args[1] + "'\n" +
"(" + String.valueOf(rowsCount) + " rows)");
}
else
{
System.out.println("Error while converting input CSV File '" + args[0] +
"' to output XML File '"+ args[1] + "'");
}
}
catch(Exception exp)
{
System.err.println(exp.toString());
}
}
}
Shankar sam
India
|