JSON2XML icon indicating copy to clipboard operation
JSON2XML copied to clipboard

Unable to generate multiple xml files out of json's.

Open digvijayah02 opened this issue 5 years ago • 4 comments

This utility is great for converting a single json but I am facing issue while trying to convert multiple json's. I have a file which contains multiple json's. Every line is a json and I need to generate that many xml files. Can you tell me what change would be required so that it reads a line converts that to a new XML file?

I have tried:- OutputStream outputStream = new FileOutputStream("Path"); then use - new JsonStreamXMLWriter(reader, new BufferedWriter(new OutputStreamWriter(outputStream))).convert();

But it throws below error:- Exception in thread "main" javax.json.stream.JsonParsingException: Expected EOF token, but got CURLYOPEN at org.glassfish.json.JsonParserImpl.hasNext(JsonParserImpl.java:361)

Attached in the input json file. input.txt ?

digvijayah02 avatar Dec 07 '19 11:12 digvijayah02

If I understand right, I think it would be easier to use shell utilities to read the text file line by line and feed each line into JSON2XML, like this:

#!/bin/bash

count=1
while IFS= read -r line; do
    echo "$line" | docker run -i -a stdin -a stdout -a stderr atomgraph/json2xml > "${count}.xml"
    count=$((count + 1))
done < "input.txt"

The script will produce XML files 1.xml, 2.xml etc, for each line of JSON.

Note that this requires a newline at the end of input.txt, otherwise the last line will not be read.

namedgraph avatar Dec 07 '19 16:12 namedgraph

Yeah, shell script would do the trick. But I was actually looking for a option to do it in java as my entire requirement is in java.

Also, let me test once by inserting “\n” manually at the end of line.

Do you have any constructor which takes input as string and not InputStream or Reader? Actually I have implemented a Spliterator which is taking care of Stream and batch of streams and then feeding me a line from my text file.

On Sat, 7 Dec 2019 at 21:44, Martynas Jusevičius [email protected] wrote:

If I understand right, I think it would be easier to use shell utilities to read the text file line by line and feed each line into JSON2XML, like this:

#!/bin/bash

count=1while IFS= read -r line; do echo "$line" | docker run -i -a stdin -a stdout -a stderr atomgraph/json2xml > "${count}.xml" count=$((count + 1))done < "input.txt"

It will produce XML files 1.xml, 2.xml etc, for each line of JSON.

Note that this requires a newline at the end of input.txt, otherwise the last line will not be read.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AtomGraph/JSON2XML/issues/2?email_source=notifications&email_token=ADASESAAVLJRFZG3STBQHPLQXPDW7A5CNFSM4JXL66XKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGGKCMY#issuecomment-562864435, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADASESCEAPP5RXQVVKVTR6TQXPDW7ANCNFSM4JXL66XA .

digvijayah02 avatar Dec 07 '19 16:12 digvijayah02

What is the problem using your Splinterator then?

Alternatively, can you read each line into a byte array byte[] and construct a ByteArrayInputStream?

namedgraph avatar Dec 07 '19 19:12 namedgraph

I did some design changes and now getting single json in a single file. This way I would avoid EOF issue and also filename issue. I am now able to read json files one by one and convert and create xml files. But after doing that 1 successfull time, it is failing in between with below error:- Exception in thread "main" javax.json.stream.JsonParsingException: Unexpected char 0 at (line no=1, column no=1, offset=0) at org.glassfish.json.JsonTokenizer.unexpectedChar(JsonTokenizer.java:601) at org.glassfish.json.JsonTokenizer.nextToken(JsonTokenizer.java:418) at org.glassfish.json.JsonParserImpl$NoneContext.getNextEvent(JsonParserImpl.java:426) at org.glassfish.json.JsonParserImpl.next(JsonParserImpl.java:376) at com.dj.gls.json2xml.JsonStreamXMLWriter.write(JsonStreamXMLWriter.java:142)

Do you have any idea why it might be throwing that?

digvijayah02 avatar Dec 09 '19 07:12 digvijayah02