14

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!

diiiz_
  • 607
  • 1
  • 6
  • 19
  • Are you doing this in the browser or with node.js or something else? – Benjamin Kovach Jul 10 '14 at 14:00
  • 1
    uhm.. parseXML converts xml to a javascript object. What else could you possibly be looking for? – Kevin B Jul 10 '14 at 14:01
  • 1
    @Benjamin Kovach In the browser. @Kevin B On the jquery page [link](http://api.jquery.com/jquery.parsexml/) it says that it returns a `XML Document`. Description: Parses a string into an XML document. I want it to create an object from xml. – diiiz_ Jul 10 '14 at 14:11
  • 1
    @KevinB Actually it converts XML to a DOM. Obviously these are JavaScript objects, but they contain sufficient metadata to accurately reconstruct the original document, e.g., by preserving node ordering. Converting to plain old JavaScript objects, which `$.parseXML` doesn't do, would lose this information. If all you want to do is consume, that might not matter though. My answer below runs through the options, but the POJO route is probably best served by JXON: https://developer.mozilla.org/en-US/docs/Archive/JXON#Reverse_Algorithms. – Bart Read May 06 '16 at 18:57
  • Some recommendations on how to convert xml to json can be found on [this question](https://stackoverflow.com/q/1773550/1743811). – doubleDown Aug 20 '17 at 15:35

4 Answers4

7

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));
});
jjrv
  • 4,211
  • 2
  • 40
  • 54
2

MDN has a good article on XML serialization and deserialisation in JavaScript, which covers all the options without having to resort to libraries such as jQuery.

Older versions of IE (pre-11) - and the current version of Opera Mini - have some limitations that may cause problems if you need to support them. Notably DOMParser.parseFromString is missing: Check this.

If you really do want to convert to JavaScript objects rather than work with a DOM, probably what you want to look at is JXON. Here's the way.

Note that if you want to recreate your XML document from objects at any point, I'd recommend you convert it to a DOM and work directly with the DOM nodes. That way, when you reserialize it, the ordering of nodes will be preserved. If you go down the plain old JavaScript object route, ordering will be lost.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Bart Read
  • 2,717
  • 3
  • 22
  • 32
1

Check out the cxsd library https://www.npmjs.com/package/cxsd it parses xsd to typescript. It has some odd dependencies. I had to do an npm install --global --production windows-build-tools also make sure cxsd.cmd is included the PATH it is a cli cxsd file.xsd it outputs a .d.ts file with the generated typescript

Moriarty
  • 515
  • 3
  • 17
0

You can also use serializeXmlNode from JSONconvert

as follow: Jsonconvert(doc)

zahma
  • 412
  • 4
  • 14