blackmagic自动将XML转换为JSON将JSON转换为XML

  • q7_382181
    了解作者
  • 139.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 09:49
    上传日期
blackmagic自动将XML转换为JSON将JSON转换为XML
blackmagic自动将XML转换为JSON将JSON转换为XML.zip
内容介绍
![XML ⇔ JS/JSON](http://nashwaan.github.io/xml-js/images/logo.svg) Convert XML text to Javascript object / JSON text (and vice versa). [![Build Status](https://ci.appveyor.com/api/projects/status/0ky9f115m0f0r0gf?svg=true)](https://ci.appveyor.com/project/nashwaan/xml-js) [![Build Status](https://travis-ci.org/nashwaan/xml-js.svg?branch=master)](https://travis-ci.org/nashwaan/xml-js) [![Build Status](https://img.shields.io/circleci/project/nashwaan/xml-js.svg)](https://circleci.com/gh/nashwaan/xml-js) [![bitHound Overall Score](https://www.bithound.io/github/nashwaan/xml-js/badges/score.svg)](https://www.bithound.io/github/nashwaan/xml-js) [![Coverage Status](https://coveralls.io/repos/github/nashwaan/xml-js/badge.svg?branch=master)](https://coveralls.io/github/nashwaan/xml-js?branch=master) [![codecov](https://codecov.io/gh/nashwaan/xml-js/branch/master/graph/badge.svg)](https://codecov.io/gh/nashwaan/xml-js) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f6ed5dd79a5b4041bfd2732963c4d09b)](https://www.codacy.com/app/ysf953/xml-js?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=nashwaan/xml-js&amp;utm_campaign=Badge_Grade) [![Code Climate](https://codeclimate.com/github/nashwaan/xml-js/badges/gpa.svg)](https://codeclimate.com/github/nashwaan/xml-js) [![npm](http://img.shields.io/npm/v/xml-js.svg)](https://www.npmjs.com/package/xml-js) [![License](https://img.shields.io/npm/l/xml-js.svg)](LICENSE) [![Downloads/month](https://img.shields.io/npm/dm/xml-js.svg)](http://www.npmtrends.com/xml-js) [![Dependency Status](https://david-dm.org/nashwaan/xml-js.svg)](https://david-dm.org/nashwaan/xml-js) [![Package Quality](http://npm.packagequality.com/shield/xml-js.svg)](http://packagequality.com/#?package=xml-js) # Synopsis ![Convert XML ↔ JS/JSON as compact or non-compact](http://nashwaan.github.io/xml-js/images/synopsis.svg) <!---![Convert XML ↔ JS/JSON as compact or non-compact](/synopsis.png?raw=true "Synopsis Diagram")--> # Features * **Maintain Order of Elements**: Most libraries will convert `<a/ rel='nofollow' onclick='return false;'><b/><a/ rel='nofollow' onclick='return false;'>` to `{a:[{},{}],b:{}}` which merges any node of same name into an array. This library can creates the following to preserve the order of elements: `{"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"},{"type":"element","name":"a"}]}`. This is very important and it is the main reason why this library was created. * **Fully XML Compliant**: Can parse: elements, attributes, texts, comments, CData, DOCTYPE, XML declarations, and Processing Instructions. * **Reversible**: Whether converting xml→json or json→xml, the result can be converted back to its original form. * **Minimal Dependencies**: This library depends only on one external npm module. * **Change Property Key Name**: Usually output of XML attributes are stored in `@attr`, `_atrr`, `$attr` or `$` in order to avoid conflicting with name of sub-elements. This library store them in `attributes`, but most importantly, you can change this to whatever you like. * **Support Upwards Traversal**: By setting `{addParent: true}` option, an extra property named `parent` will be generated along each element so that its parent can be referenced. Therefore, anywhere during the traversal of an element, its children **and** its parent can be easily accessed. * **Support Command Line**: To quickly convert xml or json files, this module can be installed globally or locally (i.e. use it as [script](https://docs.npmjs.com/misc/scripts) in package.json). * **Customize Processing using Callback Hooks**: [Custom functions](#options-for-custom-processing-functions) can be supplied to do additional processing for different parts of xml or json (like cdata, comments, elements, attributes ...etc). * **Portable Code**: Written purely in JavaScript which means it can be used in Node environment and **browser** environment (via bundlers like browserify/JSPM/Webpack). * **Typings Info Included**: Support type checking and code suggestion via intellisense. Thanks to the wonderful efforts by [DenisCarriere](https://github.com/DenisCarriere) ## Compact vs Non-Compact Most XML to JSON converters (including online converters) convert `<a/ rel='nofollow' onclick='return false;'>` to some compact output like `{"a":{}}` instead of non-compact output like `{"elements":[{"type":"element","name":"a"}]}`. While compact output might work in most situations, there are cases when elements of different names are mixed inside a parent element. Lets use `<a x="1"/ rel='nofollow' onclick='return false;'><b x="2"/><a x="3"/ rel='nofollow' onclick='return false;'>` as an example. Most converters will produce compact output like `{a:[{_:{x:"1"}},{_:{x:"3"}}], b:{_:{x:"2"}}}`, which has merged both `<a rel='nofollow' onclick='return false;'>` elements into an array! If you try to convert this back to xml, you will get `<a x="1"/ rel='nofollow' onclick='return false;'><a x="3"/ rel='nofollow' onclick='return false;'><b x="2"/>` which has not preserved the order of elements! There is an inherent limitation in the compact representation because output like `{a:{_:{x:"1"}}, b:{_:{x:"2"}}, a:{_:{x:"3"}}}` is illegal (same property name `a` should not appear twice in an object). This leaves no option but to use array `{a:[{_:{x:"1"}},{_:{x:"3"}}]`. The non-compact output, which is supported by this library, will produce more information and always guarantees the order of the elements as they appeared in the XML file. Another drawback of compact output is the resultant element can be an object or an array and therefore makes the client code a little awkward in terms of the extra check needed on object type before processing. NOTE: Although non-compact output is more accurate representation of original XML than compact version, the non-compact version is verbose and consumes more space. This library provides both options. Use `{compact: false}` if you are not sure because it preserves everything; otherwise use `{compact: true}` if you want to save space and you don't care about mixing elements of same type and losing their order. Tip: You can reduce the output size by using shorter [key names](#options-for-changing-key-names). # Usage ## Installation ``` npm install --save xml-js ``` You can also install it globally to use it as a command line convertor (see [Command Line](#command-line)). ``` npm install --global xml-js ``` ## Quick start ```js var convert = require('xml-js'); var xml = '<?xml version="1.0" encoding="utf-8"?>' + '<note importance="high" logged="true">' + ' <title>Happy</title>' + ' <todo>Work</todo>' + ' <todo>Play</todo>' + '</note>'; var result1 = convert.xml2json(xml, {compact: true, spaces: 4}); var result2 = convert.xml2json(xml, {compact: false, spaces: 4}); console.log(result1, '\n', result2); ``` To see the result of this code, see the output above in [Synopsis](#synopsis) section. Or [run and edit](https://runkit.com/587874e079a2f60013c1f5ac/587874e079a2f60013c1f5ad) this code live in the browser. ## Sample Conversions | XML | JS/JSON compact | JS/JSON non-compact | |:----|:----------------|:--------------------| | `<a/ rel='nofollow' onclick='return false;'>` | `{"a":{}}` | `{"elements":[{"type":"element","name":"a"}]}` | | `<a/ rel='nofollow' onclick='return false;'><b/>` | `{"a":{},"b":{}}` | `{"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"}]}` | | `<a rel='nofollow' onclick='return false;'><b/></a>` | `{"a":{"b":{}}}` | `{"elements":[{"type":"element","name":"a","elements":[{"type":"element","name":"b"}]}]}` | | `<a rel='nofollow' onclick='return false;'> Hi </a>` | `{"a":{"_text":" Hi "}}` | `{"elements":[{"type":"element","name":"a","elements":[{"type":"text","text":" Hi "}]}]}` | | `<a x="1.234" y="It's"/ rel='nofollow' onclick='return false;'>` | `{"a":{"_attributes":{"x":"1.234","y":"It's"}}}` | `{"elements":[{"type":"element","name":"a","attributes":{"x":"1.234","y":"It's"}}]}` | | `<?xml?>` | `{"_declaration":{}}` | `{"declaration":{}}` | | `<?go there?>` | `{"_instruction":{"go":"there"}}` | `{"elements":[{"type":"instruction","name":"go","instruction":"there"}]}` | | `<?xml version="1.0" encoding="utf-8"?>` | `{"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}}}` | `{"decl
评论
    相关推荐