How to deserialize XML to JavaScript/TypeScript?
deserialization, javascript, parsing, typescript, xml
Solution
cxml can parse XML into JSON and also fire handlers during parsing to process a file as it loads. You can compile `.xsd` schema files to TypeScript `.d.ts` definition files using cxsd. The parsed output will also be fully typed so an IDE like Atom or the `tsc` compiler can check that you're correctly handling the parsed JSON (element and attribute names and data types etc.)
Here's what using it looks like (more examples in Github):
import * as cxml from 'cxml';
import * as example from 'cxml/test/xmlns/dir-example';
var parser = new cxml.Parser();
var result = parser.parse('<dir name="empty"></dir>', example.document);
result.then((doc: example.document) => {
console.log(JSON.stringify(doc, null, 2));
});
Problem
I get a XML structure that contains information about an Appointment. My code: ``` function AppointmentCallback(appointment : any) { } ``` I want to convert this in an object in JavaScript or TypeScript(preferred). In jQuery there's only the `parseXML` method which makes the opposite from what I need. Is there any library that makes this possible? Thanks in advance!