I am trying to solve an issue with Parsing data from CSV file and Marshalling to XML. The code I wrote works almost flawless, but output is not ok, since program outputs only last line into the XML. Can please someone help me where I went wrong?
I have tried to put whole new Object into the While loop, but then it outputs last line into the XML for n-times (if CSV file has 24 lines, it will output the last line 24 times into XML)....
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.regex.Pattern;
import generated.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
public class eSpremnicaHandel {
public void eSpremnicaHandel(String csvFile) {
String line = "";
String cvsSplitBy = "|";
RegistriranaPosiljka regpos = new RegistriranaPosiljka();
Naslovnik nasl = new Naslovnik();
Posiljka pos = new Posiljka();
Storitev sto = new Storitev();
Oddaja oddaja = new Oddaja();
ArrayOfRegPosiljka regp = new ArrayOfRegPosiljka();
oddaja.setWPID("CertSubject");
oddaja.setKOMID(21553);
oddaja.setSTODD(0);
oddaja.setPOGID(2427);
oddaja.setPODID(0);
oddaja.setPOSID("7733");
oddaja.setRegistriranePosiljke(regp);
ObjectFactory fc = new ObjectFactory();
Oddaja odd = fc.createOddaja();
odd.setWPID(oddaja.getWPID());
odd.setSTODD(oddaja.getSTODD());
odd.setKOMID(oddaja.getKOMID());
odd.setPOGID(oddaja.getPOGID());
odd.setPODID(oddaja.getPODID());
odd.setPOSID(oddaja.getPOSID());
regp.getREGPOS().add(regpos);
odd.getRegistriranePosiljke();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use delimiter as separator
String[] podatki = line.split(Pattern.quote(cvsSplitBy));
regpos.setZAPST(Integer.parseInt(podatki[0]));
regpos.setREG(podatki[1]);
nasl.setNAZ(podatki[2]);
nasl.setPOSTST(podatki[3]);
pos.setMAS(new BigDecimal(podatki[4]));
sto.setNAZ(podatki[7]);
regpos.setNAS(nasl);
}
} catch (IOException e) {
e.printStackTrace();
}
try{
JAXBContext jc = JAXBContext.newInstance("generated");
JAXBElement<Oddaja> element = fc.createOddaja(oddaja);
Marshaller ms = jc.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ms.marshal(element, System.out);
ms.marshal(element, new File("src/eSpremnicaXML/test2.xml"));
} catch(Exception e) {
e.printStackTrace();
}
}
}
Please try this version of method. Main change is creating new object of RegistriranaPosiljka is moved under loop
public static void eSpremnicaHandel(String csvFile) {
String line = "";
String cvsSplitBy = "|";
Oddaja oddaja = new Oddaja();
ArrayOfRegPosiljka regp = new ArrayOfRegPosiljka();
oddaja.setWPID("CertSubject");
oddaja.setKOMID(21553);
oddaja.setSTODD(0);
oddaja.setPOGID(2427);
oddaja.setPODID(0);
oddaja.setPOSID("7733");
oddaja.setRegistriranePosiljke(regp);
ObjectFactory fc = new ObjectFactory();
Oddaja odd = fc.createOddaja();
odd.setWPID(oddaja.getWPID());
odd.setSTODD(oddaja.getSTODD());
odd.setKOMID(oddaja.getKOMID());
odd.setPOGID(oddaja.getPOGID());
odd.setPODID(oddaja.getPODID());
odd.setPOSID(oddaja.getPOSID());
odd.setRegistriranePosiljke(oddaja.getRegistriranePosiljke());
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
RegistriranaPosiljka regpos = new RegistriranaPosiljka();
// use delimiter as separator
String[] podatki = line.split(Pattern.quote(cvsSplitBy));
regpos.setZAPST(Integer.parseInt(podatki[0]));
regpos.setREG(podatki[1]);
Naslovnik nasl = new Naslovnik();
nasl.setNAZ(podatki[2]);
nasl.setPOSTST(podatki[3]);
Posiljka pos = new Posiljka();
pos.setMAS(new BigDecimal(podatki[4]));
Storitev sto = new Storitev();
sto.setNAZ(podatki[7]);
regpos.setNAS(nasl);
regp.getREGPOS().add(regpos);
}
} catch (IOException e) {
e.printStackTrace();
}
try{
JAXBContext jc = JAXBContext.newInstance("generated");
JAXBElement<Oddaja> element = fc.createOddaja(oddaja);
Marshaller ms = jc.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ms.marshal(element, System.out);
ms.marshal(element, new File("src/eSpremnicaXML/test2.xml"));
} catch(Exception e) {
e.printStackTrace();
}
}
Related
file path is not working, input1.txt is located in the same directory as library.java.
What should i do to correct it ?
How should i give path so that it read thr text file ?
package SimpleLibrarySystem;
import java.time.LocalDateTime;
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Library
{
ArrayList <Book> var = new ArrayList<Book>();
HashMap<Book, LocalDateTime> var1 = new HashMap<Book, LocalDateTime>();
public Library(String person, LocalDateTime time)
{
try{
File myfile = new File("input1.txt") ;
Scanner br = new Scanner(myfile);
String line = br.nextLine();
while ((line != null))
{
String a = line;
line = br.nextLine();
String b = line;
Book a1 = new Book(a,b,person);
Book a2 = new Book (a,b, "");
var.add(a2);
var1.put(a1,time);
//System.out.println(a + " "+ b);
line = br.nextLine();
}
br.close();
}
catch (Exception e)
{
System.out.println("not working");
}
}
}
with code:
public class Main{
public static void main(String[] args) {
int counter = 0;
try (FileReader fileReader = new FileReader("src/file.txt"); Scanner sc = new Scanner(fileReader)) {
while (sc.hasNextLine()) {
++counter;
sc.nextLine();
}
} catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
System.out.println(counter);
}
}
checkout: how to read file in Java
I'm trying to create a Java program that converts a text file to an ARFF file for Weka. Somehow my name attribute is set to numerical, but it should be set to a string. I tried everything, I tried fixing it fixing
attr.add(new Attribute("name"));
to
attr.add(new Attribute("name",true));
But when I run it, it prints the names as number (which is in the 2nd column)
1,0,?,?,?
1000,1,?,?,?
1002,2,?,?,?
2,3,?,?,?
3000,4,?,?,?
What am I doing wrong?
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import java.util.*;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
public class WekaCreateARFF {
private static final String FILENAME = "Some File";
public static void main(String[] args) throws IOException {
ArrayList<String> input = new ArrayList<String>();
ArrayList<Attribute> attr = new ArrayList<Attribute>();
Instances dataset;
double [] values;
BufferedReader br = null;
FileReader fr = null;
String date = null;
double id;
String n = null;
Instance inst = new DenseInstance(5);
List nominal_state = new ArrayList(5);
nominal_state.add("CA");
nominal_state.add("NC");
nominal_state.add("TX");
nominal_state.add("SC");
nominal_state.add("NY");
List nominal_party = new ArrayList(2);
nominal_party.add("republican");
nominal_party.add("democrat");
attr.add(new Attribute("id"));
attr.add(new Attribute("name",true));
attr.add(new Attribute("political party", nominal_party));
attr.add(new Attribute("state", nominal_state));
attr.add(new Attribute("birth date", date));
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String entry;
dataset = new Instances("SimpleARFF",attr,0);
values = new double[dataset.numAttributes()];
while ((entry = br.readLine()) != null) {
//System.out.println(entry);
input.add(entry);
for (int i = 0; i<5; i++ ) {
String[] parts = entry.split(",");
String part1 = parts[0];
String name = parts[1];
id = Double.parseDouble(part1);
inst.setValue(attr.get(0), id);
inst.setValue(attr.get(1), name);
}
System.out.println(inst);
dataset.add(new DenseInstance(1.0, values));
}
//System.out.println(dataset);
//ArffSaver arff = new ArffSaver();
//arff.setInstances(dataset);
//arff.setFile(new File("Simple.arff"));
//arff.writeBatch();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
You probably want this constructor:
http://weka.sourceforge.net/doc.dev/weka/core/Attribute.html#Attribute-java.lang.String-boolean-
That is, you essentially have to add a boolean flag to tell Weka that you want a String attribute, and not a numeric attribute (the default):
new Attribute("blah", true)
should give you a String-attribute.
I put in my csv some results Like this:
ID COUNTRY
0009 FR
0006 FR
The problem is that when I open my file.CSV,I read under ID the values 9 and 6 and not 0009 and 0006. How is it possibile and How I can avoid this problem? Anyone can help me?
(Given that the question is tagged with Java.)
If you have a CSV as mentioned, which is tab separated:
ID COUNTRY
0009 FR
0006 FR
Suppose it is located at: src/com/stackoverflow/myfile.csv
You can read it as below:
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CsvReader {
public static final String CSVFILE = "src/com/stackoverflow/myfile.csv";
public static void main(String[] args) {
BufferedReader br = null;
String line = "";
String cvsSplitBy = "\\t+"; // use tab as separator
try {
br = new BufferedReader(new FileReader(CSVFILE));
while ((line = br.readLine()) != null) {
String[] info = line.split(cvsSplitBy);
StringBuilder details = new StringBuilder();
details.append("ID = ");
details.append(info[0]);
details.append(", Country = ");
details.append(info[1]);
System.out.println(details.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
And the output would be:
ID = ID, Country = COUNTRY
ID = 0009, Country = FR
ID = 0006, Country = FR
Note: it is 0009 and 0006
UPDATED (Just Java 8 way to read the CSV file):
package com.stackoverflow;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class CsvReader {
public static final String CSVFILE = "src/com/stackoverflow/myfile.csv";
public static void main(String[] args) {
String cvsSplitBy = "\\t+"; // use tab as separator
try (Stream<String> stream = Files.lines(Paths.get(CSVFILE))) {
stream.forEach(line -> {
String[] info = line.split(cvsSplitBy);
StringBuilder details = new StringBuilder();
details.append("ID = ");
details.append(info[0]);
details.append(", Country = ");
details.append(info[1]);
System.out.println(details.toString());
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
You could escape the value as a string
ID COUNTRY
"0009" FR
"0006" FR
I believe your problem has nothing to do with Java, I'd assume you're using a tab processor (Excel, Calc, ...). When opening the file, you have to explicitly set the column type to text, otherwise it will "guess" it's a number and clean the leading zeros.
For example we have a .txt file:
Name smth
Year 2012
Copies 1
And I want to replace it with that:
Name smth
Year 2012
Copies 0
Using java.io.*.
Here is the code that does that. Let me know if you have any question.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test2 {
Map<String, String> someDataStructure = new LinkedHashMap<String, String>();
File fileDir = new File("c:\\temp\\test.txt");
public static void main(String[] args) {
Test2 test = new Test2();
try {
test.readFileIntoADataStructure();
test.writeFileFromADataStructure();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void readFileIntoADataStructure() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(fileDir)));
String line;
while ((line = in.readLine()) != null) {
if (line != null && !line.trim().isEmpty()) {
String[] keyValue = line.split(" ");
// Do you own index and null checks here this is just a sample
someDataStructure.put(keyValue[0], keyValue[1]);
}
}
in.close();
}
private void writeFileFromADataStructure() throws IOException {
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir)));
for (String key : someDataStructure.keySet()) {
// Apply whatever business logic you want to apply here
myBusinessMethod(key);
out.write(key + " " + someDataStructure.get(key) + "\n");
out.append("\r\n");
out.append("\r\n");
}
out.flush();
out.close();
}
private String myBusinessMethod(String data) {
if (data.equalsIgnoreCase("Copies")) {
someDataStructure.put(data, "0");
}
return data;
}
}
Read your original text file line by line and separate them into string tokens delimited by spaces for output, then when the part you want replaced is found (as a string), replace the output to what you want it to be. Adding the false flag to the filewrite object ("filename.txt", false) will overwrite and not append to the file allowing you to replace the contents of the file.
this is the code to do that
try {
String sCurrentLine;
BufferedReader br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yourFolder/theinputfile.txt" , false));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.indexOf("Copies")>=0){
bw.write("Copies 0")
}
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close()bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
hopefully that help
i want to sort elements in xml file according to chosen algorithm type by user in java.i have merged xml file which contains 10 xml files. so it will be like first user select type of algorithm and then program sort elements as user wants.how can i do this? pls help me
here is how i merged xml files
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Merge2 {
public static void main(String[] args) {
String sourceFile1Path = "C:\\Users\\project\\Numbers1.xml";
String sourceFile2Path = "C:\\Users\\project\\Numbers2.xml";
String sourceFile3Path = "C:\\Users\\project\\Numbers3.xml";
String sourceFile4Path = "C:\\Users\\project\\Numbers4.xml";
String sourceFile5Path = "C:\\Users\\project\\Numbers5.xml";
String sourceFile6Path = "C:\\Users\\project\\Numbers6.xml";
String sourceFile7Path = "C:\\Users\\project\\Numbers7.xml";
String sourceFile8Path = "C:\\Users\\project\\Numbers8.xml";
String sourceFile9Path = "C:\\Users\\project\\Numbers9.xml";
String sourceFile10Path = "C:\\Users\\project\\Numbers10.xml";
String mergedFilePath = "C:\\Users\\project\\mergedXML.xml";
File[] files = new File[10];
files[0] = new File(sourceFile1Path);
files[1] = new File(sourceFile2Path);
files[2] = new File(sourceFile3Path);
files[3] = new File(sourceFile4Path);
files[4] = new File(sourceFile5Path);
files[5] = new File(sourceFile6Path);
files[6] = new File(sourceFile7Path);
files[7] = new File(sourceFile8Path);
files[8] = new File(sourceFile9Path);
files[9] = new File(sourceFile10Path);
File mergedFile = new File(mergedFilePath);
mergeFiles(files, mergedFile);
}
public static void mergeFiles(File[] files, File mergedFile) {
FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(mergedFile, true);
out = new BufferedWriter(fstream);
} catch (IOException e1) {
e1.printStackTrace();
}
for (File f : files) {
System.out.println("merging: " + f.getName());
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
while ((aLine = in.readLine()) != null) {
out.write(aLine);
out.newLine();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and my merged xml file starts like below
<numberList>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<number index="1">770820</number>
<number index="2">640903</number>
<number index="3">997132</number>
<number index="4">193504</number>
i want to show it like
<numberList>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<number index="1">193504</number>
<number index="2">640903</number>
<number index="3">770820</number>
<number index="4">997132</number>
I don't understand the sort thing but just for you to know, your code could be far more concise :
String pathTemplate = "C:\\Users\\project\\Numbers%d.xml";
String mergedFilePath = "C:\\Users\\project\\mergedXML.xml";
int nFiles = 10;
File[] files = new File[nFiles];
for (int i=0 ; i<nFiles ; i++)
files[i] = new File(String.format(pathTemplate,i + 1));
//...
If you explain clearer your problem about sorting the merge file I'll complete my answer.