I have a .txt file which contains the data in json format. I would like to create mapping by using the below method, but it is showing an exception:
Root type mapping not empty after parsing (while creating mapping with dynamic template).
I would be thankful to you if you tell me what is the mistake and how to resolve it.
InputStream fileStream;
StringBuilder mapTemplate= new StringBuilder();
String line;
File mapFile = new File(mapFileBase); //mapFileBase is a string which holds the path name of the .txt file
fileStream = new FileInputStream(mapFile);
BufferedReader br = new BufferedReader(new inputStreamReader(fileStream));
while ((line = br.readLine()) != null) {
mapTemplate.append(line);
}
String mTemplate=mapTemplate.toString();
mTemplate=mTemplate.replaceAll("\n ", "").replaceAll("\\s+", "");
System.out.println(mTemplate);
createIndexRequestBuilder.addMapping(type, mappingBuilder);
// MAPPING DONE
createIndexRequestBuilder.execute().actionGet();
And I can't use
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject(type)
.startObject("properties")..........
as the json file is very huge.
Related
I downloaded an xml file from web service. If I open file from file system is formed correctly, but when I run my code isn't formed correctly.
A part of xml file formed correctly, it opened from file system:
<?xml version="1.0" encoding="UTF-8"?><ns3:FatturaElettronica xmlns:ns3="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" versione="FPR12">
Here the same xml file managed by my code:
ÿþ<
I can't copy the code and I put an image of what I see on the eclipse console.
I tryed different ways to manage this file, but nothing worked.
This is the code that it manages files. I put all ways I tryed to solve the error.
private static String readFile(File file, Writer writerArg) throws FileNotFoundException, IOException,Exception
{
FileInputStream fis = null;
InputStreamReader isr = null;
String typeEncoding = null;
/*
* First way
*
* BufferedReader br = new BufferedReader(new FileReader(fileName));
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = br.readLine()) != null)
{
// System.out.println("Writing: " + nextLine);
writerArg.write(nextLine);
// sb.append(nextLine);
sb.append(nextLine+"\n");
} // Convert the content into to a string
String clobData = sb.toString().trim();
*/
/*
* Second way
*
* fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
typeEncoding = isr.getEncoding();
Charset inputCharset = Charset.forName(typeEncoding);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), inputCharset));
String str;
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = in.readLine()) != null) {
System.out.println(nextLine);
writerArg.write(nextLine);
// sb.append(nextLine);
sb.append(nextLine+"\n");
}
String clobData = sb.toString().trim();
// Return the data.
return clobData;
*/
/* Third way */
String data = "";
data = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
System.out.println(data);
return data;
}
And when the below code receives the string I get the error: "String index out of range: -1"
schema=stringXml.substring(0,stringXml.indexOf("<FatturaElettronicaHeader")).trim();
The first way downloaded thousands of files and managed them. Only this file gives my this error. It's from yesterday that I'm looking for a way to solve the error.
Can anyone give my any idea?
I am writing a little app where I go to an api, get some json data and fill that into a csv file.
It works so far, that I get a csv file, with the correct rows, but instead of columns there are still commas in text form in file.
I am using opencsv.
private void writeCsv(InputStream input, String name) throws IOException {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
StringBuilder text = new StringBuilder();
while ((line = reader.readLine()) != null) {
text.append(line);
}
JSONArray docs = new JSONArray(text.toString());
File file=new File(name + ".csv");
String csv = CDL.toString(docs);
CSVWriter writer = new CSVWriter(new FileWriter(file));
BufferedReader sreader = new BufferedReader(new StringReader(csv));
String csvline;
while ((csvline = sreader.readLine()) != null) {
writer.writeNext(csvline);
}
writer.close();
System.out.println("done");
}
You need to add column mapping strategy.
ColumnPositionMappingStrategy mappingStrategy = new ColumnPositionMappingStrategy();
check this blog for better understanding.
http://www.javainterviewpoint.com/csvtobean-and-beantocsv-example-using-opencsv/
I generate a XML like this. It works fine. But I want to print it out in Eliscps:
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class PersonConstructor {
String info="";
String path="c://myfile/myperson";
// here is my xml object
Person person = new Person();
person.setFirstName("fname");
person.setLastName("lname");
person.setTel("111-111-1111");
person.setAddress("1000 main st.");
//Serializer my object to file.
Serializer serializer = new Persister();
File file = new File(path);
serializer.write(person, file);
//now I want to print out my xml file.I don't know how to do it.
info = file.toString();
System.out.println(info);
}
Should I use output stream?
Use a buffered reader. Just make sure to either add the IOException to your method signature or surround with try/catch block.
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null)
{
System.out.println(line);
line = in.readLine();
}
in.close();
I have xml file under ASSETS dir (com.android.project\assets\xml\file.xml).
I want to call a (following) function to read the xml file and return the contents as String.
The function requires the path of the file as string. I don't know how to give the path of the file as string.
private String getXML(String path){
String xmlString = null;
AssetManager am = this.getAssets();
try {
InputStream is = am.open(path);
int length = is.available();
byte[] data = new byte[length];
is.read(data);
xmlString = new String(data);
} catch (IOException e1) {
e1.printStackTrace();
}
return xmlString;
}
The file.xml:
<Items>
<ItemData>
<ItemNumber>ABC</ItemNumber>
<Description>Desc1</Description>
<Price>9.95</Price>
<Weight>10.00</Weight>
</ItemData>
<ItemData>
<ItemNumber>XYZ</ItemNumber>
<Description>"Desc2"</Description>
<Price>19.95</Price>
<Weight>22.22</Weight>
</ItemData>
</Items>
QUESTIONS:
How can I call the function getXML(String path) with a path as string parameter, if my file is located under \assets\xml\file.xml ?
Finally, Is there any better way of reading XML file as a String?
Thank you!
The following would work:
InputStream is = context.getAssets().open("xml/file.xml");
the path is just the path under the assets directory using forward slash (/)
so assets/x/y.z is referenced as this.getAssets().open("x/y.z");
That isnt the correct way to read the data - Inputstream.read isnt garunteed to read all the data it returns the number of bytes read - likely this will work for smaller file but you might get problems with bigger ones.
This is general code i read to to read text files, instead of a FileReader use an InputStreamReader
StringBuilder sw = new StringBuilder();
BufferedReader reader = new BufferedReader( new FileReader(file));
String readline = "";
while ((readline = reader.readLine()) != null) {
sw.append(readline);
}
String string = sw.toString();
I am using following code to write json to my local path which i get from my html page.Again I have to construct a html page by reading content from the saved local json file.For this I have to read this saved file from local which is plain text and give as input to java file. I got confused whether to use Buffered Reader or BufferedInputStream to read that file from local path.Please help me.
java.io.BufferedWriter jsonOut = new java.io.BufferedWriter(
new java.io.OutputStreamWriter(
new java.io.FileOutputStream(uploadDir +
_req.getParameter("filename")), "ISO-8859-1"));
BufferedReader for text.
Reason: http://tutorials.jenkov.com/java-io/bufferedreader.html
You can use BufferedReader for text but you should ensure to use the proper charset in your case (otherwise it defaults to the platform charset)
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile),"ISO-8859-1"));
To read a file you can use the following code
File f = new File("your json file");
BufferedReader buf = new BufferedReader(new FileReader(f));
String line = null;
while ((line = buf.readLine()) != null) {
System.out.println("json file line " + line);
// do your changes
}