lantanagroup / FHIR.js

Node.JS library for serializing/deserializing FHIR resources between JS/JSON and XML using various node.js XML libraries
Apache License 2.0
104 stars 29 forks source link

XML file to JSON #25

Closed neilpalima closed 4 years ago

neilpalima commented 4 years ago

Is there a way to convert a fhir xml file to json? On your example it always starts from a json object.

We have a fhir xml file that needs to be converted to a json.

seanmcilvenna commented 4 years ago

Yes, you must read the file before calling FHIR.js, though. Here is an example... Maybe not perfect, cause I'm writing this off of memory, but should get you going in the right direction.

var fs = require('fs');
var {Fhir} = require('fhir');

var sourceXmlFile = 'path to source xml';
var destJsonFile = 'path to destination json';

var fhir = new Fhir();
var xml = fs.readFileSync(sourceXmlFile);
var obj = fhir.xmlToObj(xml);
var json = JSON.stringify(obj, null, '\t');
fs.writeFileSync(destJsonFile, json);