如何将XML反序列化为JavaScript / TypeScript? [英] How to deserialize XML to JavaScript/TypeScript?

查看:606
本文介绍了如何将XML反序列化为JavaScript / TypeScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个XML结构,其中包含有关约会的信息。

I get a XML structure that contains information about an Appointment.

我的代码:

function AppointmentCallback(appointment : any) {
}

I想要在JavaScript或TypeScript(首选)中的对象中转换它。在jQuery中,只有 parseXML 方法与我需要的方法相反。

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?

提前致谢!

推荐答案

cxml 可以将XML解析为解析期间JSON和fire处理程序在加载文件时处理文件。您可以使用 .xsd 模式文件编译为TypeScript .d.ts 定义文件/github.com/charto/cxsd\"rel =nofollow> cxsd 。解析后的输出也将完全输入,因此像Atom这样的IDE或 tsc 编译器可以检查您是否正确处理解析的JSON(元素和属性名称以及数据类型等) 。)

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.)

这是使用它的样子(更多例子 in Github ):

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));
});

这篇关于如何将XML反序列化为JavaScript / TypeScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆