• q7_382181
  • 139.7KB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-06-15 09:49
![XML ⇔ JS/JSON]( Convert XML text to Javascript object / JSON text (and vice versa). [![Build Status](]( [![Build Status](]( [![Build Status](]( [![bitHound Overall Score](]( [![Coverage Status](]( [![codecov](]( [![Codacy Badge](](;utm_medium=referral&amp;utm_content=nashwaan/xml-js&amp;utm_campaign=Badge_Grade) [![Code Climate](]( [![npm](]( [![License](](LICENSE) [![Downloads/month](]( [![Dependency Status](]( [![Package Quality](]( # Synopsis ![Convert XML ↔ JS/JSON as compact or non-compact]( <!---![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]( 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]( ## 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]( 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