How do I output all the JSON lines into one XML output? My code only outputs the first JSON line.
I have a data file that contains several JSON lines in them (CR & LF at end of each JSON line):
{"valueName":"GPS_latitude","valueType":"-1","value":"39.26842508","objectID":"charger_80","timestamp":"1556841594000"}
{"valueName":"GPS_longitude","valueType":"-1","value":"-76.60410104","objectID":"charger_80","timestamp":"1556841594000"}
{"valueName":"GPS_altitude","valueType":"-1","value":"13","objectID":"charger_80","timestamp":"1556841594000"}
My java src looks like:
import org.json.JSONObject;
import org.json.XML;
//Reads in the file and makes it one big string (which works correctly)
String jsonData = readFile("testfilePD.json");
//Converts the JSON string into XML but stops after first line.
String jsonBody = Convert_JSON_TO_XML.convert_json(jsonData); System.out.println(jsonBody);
//I'm using the XML library to convert
public static String convert_json(String json_value) {
String xml = "<node>";
try {
JSONObject jsoObject = new JSONObject(json_value);
xml = xml + XML.toString(jsoObject);
} catch (Exception e) {
System.out.println(e);
}
xml = xml + "</node>";
return xml;
}
Or, is there a better JSON to XML conversion library? Or how do I modify the XML library to read my entire file and output them in one big XML file correctly. I'm eventually sending this xml file to a web service API for ingestion. I'm stuck.
Output should be something like:
<?xml version="1.0"?>
<node>
<valueName>GPS_latitude</valueName>
<valueType>-1</valueType>
<value>39.26842508</value>
<objectID>charger_80</objectID>
<timestamp>1556841594000</timestamp>
</node>
<node>
<valueName>GPS_longitude</valueName>
<valueType>-1</valueType>
<value>-76.60410104</value>
<objectID>charger_80</objectID>
<timestamp>1556841594000</timestamp>
</node>