JSON to XML conversion

Sunday, 2 February 2020 · 2 messages · last reply 3 Feb 2020
T
thufir2 Feb 2020 03:00 #
Hi, How do I do something like:
     public void transform(String fileName) throws IOException {
         String content = new
String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);
         org.json.JSONObject json = new org.json.JSONObject(content);
         log.info(org.json.XML.toString(json));
     }
but using BaseX as below?
     private void baseXparseJsonFile(String fileName) throws IOException   {
         org.basex.build.json.JsonParser jsonParser = new
org.basex.build.json.JsonParser(new IOFile(fileName), new MainOptions());
         //where is the xml?
     }
Unclear on how to use the different parsers (really, just need this one parser). See also: https://stackoverflow.com/q/60022419/262852 thanks, Thufir
CG
Christian Grün3 Feb 2020 15:33 #
Quoted text · thufir · 6 lines
> public void transform(String fileName) throws IOException { > String content = new > String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8); > org.json.JSONObject json = new org.json.JSONObject(content); > log.info(org.json.XML.toString(json)); > }
What you seem to want to achieve is:
1. Open a JSON file as a string;
2. Convert this string to a JSON object;
3. Write this JSON object as XML to a log output (?)
This would be the XQuery way to do it:
  let $content := file:read-text('x.json')
  let $json := json:parse($content)
  return admin:write-log($json)
If you address the BaseX Java code, you can work with different abstraction levels. Maybe it’s already sufficient if you evaluate the upper XQuery string as command:
  Context ctx = new Context();
  String query = "let $content...";
  XQuery cmd = new XQuery(query);
  System.out.println(cmd.execute(ctx));

⚡Generated with XQuery