Skip to content

Commit

Permalink
support commentPropName when order is not given
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Mar 16, 2022
1 parent 0ee2a31 commit ec91735
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
53 changes: 52 additions & 1 deletion docs/v4/3.XMLBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,58 @@ Output

It is recommended to use `preserveOrder: true` when you're parsing XML to js object and building the XML back. So that the order of CDATA is maintained.
## commentPropName
To recognize commentsin a JS object so that they can be transformed correcty. This option is supported only if `preserveOrder: true` because position of comment is important.
To recognize comments in a JS object so that they can be transformed correcty.


Eg
Input
```json
{
"any_name": {
"person": {
"phone": [
122233344550,
122233344551,
""
],
"name": [
"<some>Jack</some>Jack",
"<some>Mohan</some>"
],
"blank": "",
"regx": "^[ ].*$"
}
}
};
```
code
```js
const options = {
processEntities:false,
format: true,
ignoreAttributes: false,
commentPropName: "phone"
};

const builder = new XMLBuilder(options);
const xmlOutput = builder.build(input);
```
Output
```xml
<any_name>
<person>
<!--122233344550-->
<!--122233344551-->
<!---->
<name><some>Jack</some>Jack</name>
<name><some>Mohan</some></name>
<blank></blank>
<regx>^[ ].*$</regx>
</person>
</any_name>`;
```

It is recommended to use `preserveOrder: true` when you're parsing XML to js object and building the XML back. So that the order of comment is maintained.

## format
By default, parsed XML is single line XML string. By `format: true`, you can format it for better view.
Expand Down
46 changes: 46 additions & 0 deletions spec/comments_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,53 @@ describe("Comments", function() {
expect(output.replace(/\s+/g, "")).toEqual(XMLdata.replace(/\s+/g, ""));
});

it("should build XML with Comments without parseOrder", function() {
const input = {
"any_name": {
"person": {
"phone": [
122233344550,
122233344551,
""
],
"name": [
`<some>Jack</some>Jack`,
`<some>Mohan</some>`
],
"blank": "",
"another": {
"phone": "1245789",
}
}
}
};
const expected = `
<any_name>
<person>
<!--122233344550-->
<!--122233344551-->
<!---->
<name><some>Jack</some>Jack</name>
<name><some>Mohan</some></name>
<blank></blank>
<another>
<!--1245789-->
</another>
</person>
</any_name>`;

const options = {
processEntities:false,
format: true,
ignoreAttributes: false,
commentPropName: "phone"
};

const builder = new XMLBuilder(options);
const xmlOutput = builder.build(input);
// console.log(xmlOutput);
expect(xmlOutput.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});

});

9 changes: 6 additions & 3 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ function buildObjectNode(val, key, attrStr, level) {

if (attrStr && val.indexOf('<') === -1) {
return ( this.indentate(level) + '<' + key + attrStr + piClosingChar + '>' + val + tagEndExp );
} else {
} else if (this.options.commentPropName !== false && key === this.options.commentPropName && piClosingChar.length === 0) {
return this.indentate(level) + `<!--${val}-->` + this.newLine;
}else {
return (
this.indentate(level) + '<' + key + attrStr + piClosingChar + this.tagEndChar +
val +
Expand All @@ -192,8 +194,9 @@ function buildEmptyObjNode(val, key, attrStr, level) {

function buildTextValNode(val, key, attrStr, level) {
if (this.options.cdataPropName !== false && key === this.options.cdataPropName) {
if(this.options.format) return this.indentate(level) + `<![CDATA[${val}]]>\n`;
else return this.indentate(level) + `<![CDATA[${val}]]>`;
return this.indentate(level) + `<![CDATA[${val}]]>` + this.newLine;
}else if (this.options.commentPropName !== false && key === this.options.commentPropName) {
return this.indentate(level) + `<!--${val}-->` + this.newLine;
}else{
let textValue = this.options.tagValueProcessor(key, val);
textValue = this.replaceEntitiesValue(textValue);
Expand Down

0 comments on commit ec91735

Please sign in to comment.