forked from NaturalIntelligence/fast-xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c041a0a
commit 5260c56
Showing
9 changed files
with
432 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Please raise an issue or PR (recommended) to add your name to our User's list | ||
|
||
<a href="https://github.com/NaturalIntelligence/imglab" title="imglab" ><img src="https://github.com/NaturalIntelligence/imglab/blob/master/img/imglab_logo.png?raw=true" width="80px" ></a> | ||
<a href="https://github.com/NaturalIntelligence/Stubmatic" title="stubmatic" ><img src="https://camo.githubusercontent.com/ff711425dc2286cd215637b7114eb43e571f001d/68747470733a2f2f6e61747572616c696e74656c6c6967656e63652e6769746875622e696f2f537475626d617469632f696d672f737475626d617469635f6c6f676f2e706e673f7261773d74727565" width="80px" ></a> | ||
<a href="https://github.com/muneem4node/muneem" title="Muneem" ><img src="https://github.com/muneem4node/muneem/raw/master/static/muneem.png?raw=true" width="80px" ></a> | ||
<a href="https://github.com/badges/shields" title="shields" ><img src="https://avatars2.githubusercontent.com/u/6254238" width="80px" ></a> | ||
<a href="https://github.com/renovatebot/renovate" title="renovate" ><img src="https://avatars1.githubusercontent.com/u/38656520" width="80px" ></a> | ||
<a href="https://vmware.com/" title="vmware" > <img src="https://avatars0.githubusercontent.com/u/473334" width="80px" ></a> | ||
<a href="https://opensource.microsoft.com/" title="microsoft" > <img src="https://avatars0.githubusercontent.com/u/6154722" width="80px" ></a> | ||
<a href="https://github.com/notable/notable" title="notable" > <img src="https://avatars3.githubusercontent.com/u/46467536" width="80px" ></a> | ||
<a href="http://ibm.github.io/" title="IBM" > <img src="https://avatars2.githubusercontent.com/u/1459110" width="80px" ></a> | ||
<a href="https://www.ft.com/" title="Financial Times" > <img src="https://avatars2.githubusercontent.com/u/3502508" width="80px" ></a> | ||
<a href="https://github.com/camunda" title="camunda BPM" > <img src="https://avatars3.githubusercontent.com/u/2443838" width="80px" ></a> | ||
<a href="https://github.com/AnyChart" title="AnyChart" > <img src="https://avatars0.githubusercontent.com/u/703373" width="80px" ></a> | ||
<a href="https://github.com/magda-io" title="magda-io" > <img src="https://avatars0.githubusercontent.com/u/40348684" width="80px" ></a> | ||
<a href="https://github.com/geistinteractive" title="Geist Interactive" > <img src="https://avatars0.githubusercontent.com/u/11617965" width="80px" ></a> | ||
<a href="https://www.tourstream.eu/" title="tourstream" > <img src="https://avatars1.githubusercontent.com/u/23242088" width="80px" ></a> | ||
<a href="https://www.atomist.com/" title="Atomist" > <img src="https://avatars3.githubusercontent.com/u/19392" width="80px" ></a> | ||
<a href="http://www.opuscapita.com/" title="OpusCapita" > <img src="https://avatars1.githubusercontent.com/u/23256480" width="80px" ></a> | ||
<a href="https://nevatrip.ru/" title="nevatrip" > <img src="https://avatars2.githubusercontent.com/u/35730984" width="80px" ></a> | ||
<a href="http://www.smartbear.com" title="SmartBear Software" > <img src="https://avatars2.githubusercontent.com/u/1644671" width="80px" ></a> | ||
<a href="http://eosnavigator.com/" title="nevatrip" > <img src="https://avatars1.githubusercontent.com/u/40260563" width="80px" ></a> | ||
<a href="http://nasa.github.io/" title="NASA" > <img src="https://avatars0.githubusercontent.com/u/848102" width="80px" ></a> | ||
<a href="http://qgis.org/" title="QGIS" > <img src="https://avatars2.githubusercontent.com/u/483444" width="80px" ></a> | ||
<a href="http://www.craft.ai/" title="craft ai" > <img src="https://avatars1.githubusercontent.com/u/12046764" width="80px" ></a> | ||
<a href="http://brownspace.org/" title="Brown Space Engineering" > <img src="https://avatars2.githubusercontent.com/u/5504507" width="80px" ></a> | ||
<a href="http://www.appcelerator.com/" title="Team Appcelerator" > <img src="https://avatars1.githubusercontent.com/u/82188" width="80px" ></a> | ||
<a href="https://xmllint.com/" title="XML Lint" > <img src="https://xmllint.com/assets/logo.png" width="80px" ></a> | ||
<a href="https://github.com/prettier" title="Prettier" > <img src="https://avatars0.githubusercontent.com/u/25822731" width="80px" ></a> | ||
<a href="https://github.com/dolanmiu/docx" title="docx" > <img src="https://i.imgur.com/37uBGhO.gif" width="80px" ></a> | ||
<a href="http://orange-opensource.github.io/" title="Open Source by Orange" > <img src="https://avatars3.githubusercontent.com/u/1506386" width="80px" ></a> | ||
<a href="http://www.ybrain.com/" title="YBRAIN Inc." > <img src="https://avatars2.githubusercontent.com/u/38232440" width="80px" ></a> | ||
<a href="http://99bitcoins.com/" title="99 bitcoins" > <img src="https://avatars0.githubusercontent.com/u/9527779" width="80px" ></a> | ||
<a href="https://wechaty.github.io/wechaty/" title="Wechaty" > <img src="https://avatars0.githubusercontent.com/u/21285357" width="80px" ></a> | ||
<a href="https://opendatakit.org" title="Open Data Kit" > <img src="https://avatars0.githubusercontent.com/u/6222985" width="80px" ></a> | ||
<a href="https://ridibooks.com" title="RIDI Books" > <img src="https://avatars1.githubusercontent.com/u/24955411" width="80px" ></a> | ||
<a href="http://signalk.org" title="Signal K" > <img src="https://avatars1.githubusercontent.com/u/7126740" width="80px" ></a> | ||
<a href="http://brain.js.org/" title="brain.js" > <img src="https://avatars2.githubusercontent.com/u/23732838" width="80px" ></a> | ||
<a href="https://skygear.io/" title="Skegear" > <img src="https://avatars1.githubusercontent.com/u/15025887" width="80px" ></a> | ||
<a href="https://npmjs.com/" title="npm" > <img src="https://avatars0.githubusercontent.com/u/6078720" width="80px" ></a> | ||
<a href=" https://www.mindpointgroup.com" title="mindpointgroup" > <img src="https://avatars1.githubusercontent.com/u/6413533" width="80px" ></a> | ||
<a href="http://www.acuantcorp.com/" title="Acuant Inc" > <img src="https://avatars3.githubusercontent.com/u/11580319?s=200&v=4" width="80px" ></a> | ||
<a href="https://www.wazuh.com/" title="wazuh" > <img src="https://avatars2.githubusercontent.com/u/13752566" width="80px" ></a> | ||
<a href="https://orbs.com/" title="ORBS The Hybrid Blockchain" > <img src="https://avatars1.githubusercontent.com/u/33665977" width="80px" ></a> | ||
<a href="https://texlab.netlify.com/" title="latex-lsp" > <img src="https://avatars1.githubusercontent.com/u/48360002" width="80px" ></a> | ||
<a href="https://frontside.io/" title="The Frontside " > <img src="https://avatars1.githubusercontent.com/u/223096" width="80px" ></a> | ||
<a href="https://creditsense.com.au/" title="Credit Sense Australia " > <img src="https://avatars0.githubusercontent.com/u/46947118" width="80px" ></a> | ||
<a href="https://www.hustunique.com/" title="UniqueStudio" > <img src="https://avatars1.githubusercontent.com/u/4847684" width="80px" ></a> | ||
<a href="http://www.openforis.org/" title="Open Foris" > <img src="https://avatars2.githubusercontent.com/u/1212750" width="80px" ></a> | ||
<a href="#" title="NHS Connect" > <img src="https://avatars3.githubusercontent.com/u/20316669" width="80px" ></a> | ||
<a href="https://tradle.io/" title="Tradle" > <img src="https://avatars2.githubusercontent.com/u/9482126" width="80px" ></a> | ||
<a href="http://www.anl.gov/" title="Argonne National Laboratory" > <img src="https://avatars0.githubusercontent.com/u/10468712" width="80px" ></a> | ||
<a href="https://simpleicons.org/" title="Simple Icons" > <img src="https://avatars2.githubusercontent.com/u/29872746" width="80px" ></a> | ||
<a href="https://stoplight.io/" title="Stoplight" > <img src="https://avatars1.githubusercontent.com/u/10767217" width="80px" ></a> | ||
<a href="http://www.fda.gov/" title="Food and Drug Administration " > <img src="https://avatars2.githubusercontent.com/u/6471964" width="80px" ></a> | ||
<a href="http://www.magento.com/" title="Magento" > <img src="https://avatars2.githubusercontent.com/u/168457" width="80px" ></a> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,240 @@ | ||
XML Builder can be used to parse a JS object into XML. It supports following options; | ||
|
||
* attributeNamePrefix | ||
* attributesGroupName | ||
* attributeValueProcessor | ||
* cdataTagName | ||
* format | ||
* ignoreAttributes | ||
* indentBy | ||
* suppressEmptyNode | ||
* tagValueProcessor | ||
* textNodeName | ||
```js | ||
const {XMLBuilder} = require('fast-xml-parser'); | ||
|
||
const options = { | ||
ignoreAttributes : false | ||
}; | ||
|
||
const builder = new XMLBuilder(options); | ||
let xmlDataStr = builder.build(jObj); | ||
``` | ||
|
||
|
||
## arrayNodeName | ||
When you build XML from an array, it's better to set `arrayNodeName` option to some name. | ||
|
||
```js | ||
const cars = [ | ||
{ | ||
"color": "purple", | ||
"type": "minivan", | ||
"registration": "2020-02-03", | ||
"capacity": 7 | ||
}, | ||
{ | ||
"color": "orange", | ||
"type": "SUV", | ||
"registration": "2021-05-17", | ||
"capacity": 4 | ||
}, | ||
]; | ||
const builder = new XMLBuilder({ | ||
arrayNodeName: "car" | ||
}); | ||
const output = builder.build(cars); | ||
``` | ||
```xml | ||
<car> | ||
<color>purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car> | ||
<car> | ||
<color>orange</color> | ||
<type>SUV</type> | ||
<registration>2021-05-17</registration> | ||
<capacity>4</capacity> | ||
</car> | ||
``` | ||
|
||
## attributeNamePrefix | ||
To recognize attribute properties in the JS object so that they can be trimmed. | ||
|
||
## attributesGroupName | ||
To recognize attribute properties group in the JS object so that they can be trimmed and can form attribute expresstion for a tag. | ||
|
||
## attributeValueProcessor | ||
To customize the bahaviour of parsing an attribute value to the string value. It accepts attribute name and value. | ||
|
||
## cdataTagName | ||
To recognize CDATA properties in a JS object so that they can be transformed correcty. This option is useful only if `preserveOrder: true` | ||
|
||
## format | ||
By default, parsed XML is single line XML string. By `format: true`, you can format it for better view. | ||
|
||
## ignoreAttributes | ||
Don't consider attributes while building XML. Other attributes related properties should be set to correctly identifying an attribute property. | ||
|
||
## indentBy | ||
Applicable only if `format:true` is set. | ||
|
||
## preserveOrder | ||
When you parse a XML using XMLParser with `preserveOrder: true`, the result JS object has different structure. So parse that structure in original XML, you should set the same option while building the XML from that js object. | ||
|
||
## suppressEmptyNode | ||
Tags with no text value would be parsed as empty tags. | ||
Input | ||
```js | ||
const builder = new XMLBuilder({ | ||
arrayNodeName: "any", //not effective | ||
suppressEmptyNode: true | ||
}); | ||
const output = builder.build({ | ||
a: 32, | ||
b: "" | ||
}); | ||
``` | ||
Outout | ||
```xml | ||
<a>32</a> | ||
<b/> | ||
``` | ||
|
||
## tagValueProcessor | ||
To customize the bahaviour of parsing the text value of a tag to the string value. It accepts tag name and value. | ||
|
||
## textNodeName | ||
To recognize text value for a tag in the JS object so that they can be properly assigned to the tag. | ||
|
||
# Restoring original XML | ||
|
||
**Example 1** | ||
|
||
When you use XML Parser with `alwaysCreateTextNode: true`, it doesn't impact XMLBuilder result | ||
|
||
```js | ||
const XMLdata = ` | ||
<car> | ||
<color>purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car>`; | ||
|
||
const parser = new XMLParser({ | ||
alwaysCreateTextNode: true, | ||
}); | ||
let result = parser.parse(XMLdata); | ||
// console.log(JSON.stringify(result, null,4)); | ||
|
||
const builder = new XMLBuilder({ format: true }); | ||
const output = builder.build(result); | ||
``` | ||
Output | ||
```xml | ||
<car> | ||
<color>purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car> | ||
``` | ||
|
||
**Example 2** | ||
|
||
When you use XML Parser with `isArray`, it doesn't impact XMLBuilder result under some extent. | ||
|
||
|
||
```js | ||
const XMLdata = ` | ||
<car> | ||
<color>purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car>`; | ||
|
||
const parser = new XMLParser({ | ||
isArray: (tagName, jPath, isLeafNode, isAttribute) => { | ||
if(isLeafNode) return true; | ||
} | ||
}); | ||
let result = parser.parse(XMLdata); | ||
// console.log(JSON.stringify(result, null,4)); | ||
|
||
const builder = new XMLBuilder(); | ||
const output = builder.build(result); | ||
``` | ||
Output | ||
```xml | ||
<car> | ||
<color>purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car> | ||
``` | ||
|
||
**Example 3** | ||
|
||
When you use XML Parser with `preserveOrder`, you should use the same option with XMLBuilder. | ||
|
||
|
||
```js | ||
const XMLdata = ` | ||
<car> | ||
<color>purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car>`; | ||
|
||
const parser = new XMLParser({ | ||
preserveOrder: true | ||
}); | ||
let result = parser.parse(XMLdata); | ||
// console.log(JSON.stringify(result, null,4)); | ||
|
||
const builder = new XMLBuilder({ preserveOrder: true }); | ||
const output = builder.build(result); | ||
``` | ||
Output | ||
```xml | ||
<car> | ||
<color>purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car> | ||
``` | ||
|
||
**Example 4** | ||
|
||
You should set `attributeNamePrefix` and other properties to the same value for XML Parser and XMLBuilder. | ||
|
||
|
||
```js | ||
const XMLdata = ` | ||
<car> | ||
<color alpha="7">purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car>`; | ||
|
||
const options = { | ||
ignoreAttributes: false, | ||
attributeNamePrefix: "@@", | ||
format: true | ||
}; | ||
const parser = new XMLParser(options); | ||
let result = parser.parse(XMLdata); | ||
// console.log(JSON.stringify(result, null,4)); | ||
|
||
const builder = new XMLBuilder(options); | ||
const output = builder.build(result); | ||
``` | ||
Output | ||
```xml | ||
<car> | ||
<color alpha="7">purple</color> | ||
<type>minivan</type> | ||
<registration>2020-02-03</registration> | ||
<capacity>7</capacity> | ||
</car> | ||
``` | ||
|
||
[> Next: XmlValidator](./4.XMLValidator.md) |
Oops, something went wrong.