I have XML file with a lot of records in XML.
for example,
<ChinRecord>
<p>(..)(..)(..)(..)(..)(......)</p>
<CP>
<p>(..)(..)(..)(..)(..)(......)</p>
</CP>
<origin>30Ntr431_C.TXT</origin>
<What>t</What>
<TZ>q84393</TZ>
<wQ>WQ</wQ>
<time>2009-11-4</time>
</ChinRecord>
<ChinRecord>
<p>(..)(..)(..)(..)(..)(......)</p>
<CP>
<p>(..)(..)(..)(..)(..)(......)</p>
</CP>
<origin>30Ntr431_C.TXT</origin>
<What>true</What>
<TZ>7027AEC</TZ>
<wQ>WQ</wQ>
<time>2009-11-30</time>
</ChinRecord>
I use the next line:
Chin newRecord = (Chin)xstream.fromXML(xml);
(I have datatype of Chin)
and It's really give me the first record of the xml in newRecord . But What I do when I want to save all the records? I create new Vector, but how I acsses all the records in the file?
Thanks.
Maybe you can use the ObjectInputStream of XStream as described here: http://x-stream.github.io/objectstream.html
But this would require to wrap your ChinRecordelements into some XML root:
<object-stream>
<ChinRecord>
..
</ChinRecord>
<ChinRecord>
..
</ChinRecord>
..
</object-stream>
Related
One of the common tasks in Spring Batch it is used for is parsing CSV files, but CSV files come in all form and shapes… and some of them are syntactically incorrect.I am currently try to parse an invalid CSV file:
My CSV file data.CSV:
"1;13/4/1992;16/7/2006"
"2;31/5/1993;1/8/2009"
"3;21/4/1990;27/4/2010"
I used this code here.
This works for me but I want to use a CSV of my own! any suggestion please?
As I said in the comment of the answer your linked, you can use a FlatFileItemReader instead of the ListItemReader and it should work. I used the ListItemReader in order to provide a self-contained example you can run (without the need to upload a csv file, etc). The point of the other question was about how to trim the leading/trailing " before parsing the lines, and the trick was to use a composite item processor as shown in the example.
You don't need to create a new reader, here is an example of a FlatFileItemReader you can use with the same example:
#Bean
public FlatFileItemReader<String> itemReader() {
return new FlatFileItemReaderBuilder<String>()
.name("itemReader")
.resource(new FileSystemResource("/absolute/path/to/file"))
.lineMapper(new PassThroughLineMapper())
.build();
}
Hope this helps.
I'm new to Google Dataflow, and can't get this thing to work with JSON. I've been reading throughout the documentation, but can't solve my problem.
So, following the WordCount example i figured how data is loaded from .csv file with next line
PCollection<String> input = p.apply(TextIO.Read.from(options.getInputFile()));
where inputFile in .csv file from my gcloud bucket. I can transform read lines from .csv with:
PCollection<TableRow> table = input.apply(ParDo.of(new ExtractParametersFn()));
(Extract ParametersFn defined by me). So far so good!
But then I realize my .csv file is too big and had to convert it to JSON (https://cloud.google.com/bigquery/preparing-data-for-bigquery).
Since BigQueryIO is supposedly better for reading JSON, I tried with the following code:
PCollection<TableRow> table = p.apply(BigQueryIO.Read.from(options.getInputFile()));
(inputFile is then JSON file and the output when reading with BigQuery is PCollection with TableRows) I tried with TextIO too (which returns PCollection with Strings) and neither of the two IO options work.
What am I missing? The documentation is really not that detailed to find an answer there, but perhaps some of you guys already dealt with this problem before?
Any suggestions would be very appreciated. :)
I believe there are two options to consider:
Use TextIO with TableRowJsonCoder to ingest the JSON files (e.g., like it is done in the TopWikipediaSessions example);
Import the JSON files into a bigquery table (https://cloud.google.com/bigquery/loading-data-into-bigquery), and then use BigQueryIO.Read to read from the table.
I want to format the output XML generated by Xstream, to make it more readable. Currently, a newline is added after each element but I would like newline to be added after every attribute. Is there a way to do this?
Pretty Print Writer is used by default to format the output of the xml but this doesn't suffice for me. I want newline to be added after every
XStream includes a PrettyPrintWriter
After building your XStream...
XStream xstream = //...whatever
Instead of:
// p is my object needing xml serialization
xstream.toXML(p)
Use something like this to make it pretty:
BufferedOutputStream stdout = new BufferedOutputStream(System.out);
xstream.marshal(p, new PrettyPrintWriter(new OutputStreamWriter(stdout)));
Take a look at their tutorial on tweaking the output.
I've used this to :
xstream= new XStream(new DomDriver());
But it's not so efficient than StaxDriver()
What I need is point 3:
Take one xsd file "testField.xsd" and parse it using "com.codename1.xml.XMLParser"
alter this xsd file and generate result
now I want to save this xsd file with name "testValues.xsd"
What i have done so far :
(1). Take one xsd file "testField.xsd" and parse it using "com.codename1.xml.XMLParser"
InputStream is = Display.getInstance().getResourceAsStream(null,
"/testField.xsd"+fileName);
Element response = xp.parse(new InputStreamReader(is));
(2). alter this xsd file and generate result
for(FormData fromDataObj : listInputs){
response.getElementById(fromDataObj.getInputId()).setAttribute("value",fromDataObj.getInputValue());
}
(3). now I want to save this xsd file with name testValues.xsd
//want to save above response as "testValues.xsd"
Look at the XmlWriter class it has the ability to write Element's to XML.
I'm working on a project under which i have to take a raw file from the server and convert it into XML file.
Is there any tool available in java which can help me to accomplish this task like JAXP can be used to parse the XML document ?
I guess you will need your objects for later use ,so create MyObject that will be some bean that you will load the values form your Raw File and you can write this to someFile.xml
FileOutputStream os = new FileOutputStream("someFile.xml");
XMLEncoder encoder = new XMLEncoder(os);
MyObject p = new MyObject();
p.setFirstName("Mite");
encoder.writeObject(p);
encoder.close();
Or you con go with TransformerFactory if you don't need the objects for latter use.
Yes. This assumes that the text in the raw file is already XML.
You start with the DocumentBuilderFactory to get a DocumentBuilder, and then you can use its parse() method to turn an input stream into a Document, which is an internal XML representation.
If the raw file contains something other than XML, you'll want to scan it somehow (your own code here) and use the stuff you find to build up from an empty Document.
I then usually use a Transformer from a TransformerFactory to convert the Document into XML text in a file, but there may be a simpler way.
JAXP can also be used to create a new, empty document:
Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.newDocument();
Then you can use that Document to create elements, and append them as needed:
Element root = dom.createElement("root");
dom.appendChild(root);
But, as Jørn noted in a comment to your question, it all depends on what you want to do with this "raw" file: how should it be turned into XML. And only you know that.
I think if you try to load it in an XmlDocument this will be fine