Skip to content

Commit

Permalink
rename cdataTagName to cdataPropName & support comments
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Nov 18, 2021
1 parent f379c66 commit 45b912f
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 42 deletions.
102 changes: 98 additions & 4 deletions docs/v4/2.XMLparseOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ const options = {
:
```

## cdataTagName
If `cdataTagName` is not set to some property name, then CDATA values are merged to tag's text value.
## cdataPropName
If `cdataPropName` is not set to some property name, then CDATA values are merged to tag's text value.

Eg
```js
Expand All @@ -152,12 +152,12 @@ Output
}
}
```
When `cdataTagName` is set to some property then text value and CDATA are parsed to different properties.
When `cdataPropName` is set to some property then text value and CDATA are parsed to different properties.

Example 2
```js
const options = {
cdataTagName: "__cdata"
cdataPropName: "__cdata"
}
```
Output
Expand All @@ -174,7 +174,44 @@ Output
}
}
```
## commentPropName
If `commentPropName` is set to some property name, then comments are parsed from XML.

Eg
```js
const xmlData = `
<!--Students grades are uploaded by months-->
<class_list>
<student>
<!--Student details-->
<!--A second comment-->
<name>Tanmay</name>
<grade>A</grade>
</student>
</class_list>`;

const options = {
commentPropName: "#comment"
};
const parser = new XMLParser(options);
const output = parser.parse(xmlDataStr);
```
Output
```json
{
"#comment": "Students grades are uploaded by months",
"class_list": {
"student": {
"#comment": [
"Student details",
"A second comment"
],
"name": "Tanmay",
"grade": "A"
}
}
}
```

## ignoreAttributes

Expand Down Expand Up @@ -403,8 +440,65 @@ Output
}
}
```
## preserveOrder
When `preserveOrder: true` , you get some long ugly result. That's because this option is used to keep the order of tags in result js Object. It also helps XML builder to build the similar kind of XML from the js object without losing information.

```js
const XMLdata = `
<!--Students grades are uploaded by months-->
<class_list>
<student>
<!--Student details-->
<!--A second comment-->
<name>Tanmay</name>
<grade>A</grade>
</student>
</class_list>`;

const options = {
commentPropName: "#comment",
preserveOrder: true
};
const parser = new XMLParser(options);
let result = parser.parse(XMLdata);
```
```json
[
{
"#comment": [
{ "#text": "Students grades are uploaded by months" }
]
},
{
"class_list": [
{
"student": [
{
"#comment": [
{ "#text": "Student details" }
]
},
{
"#comment": [
{ "#text": "A second comment" }
]
},
{
"name": [
{ "#text": "Tanmay" }
]
},
{
"grade": [
{ "#text": "A" }
]
}
]
}
]
}
]
```
## removeNSPrefix

Remove namespace string from tag and attribute names.
Expand Down
6 changes: 4 additions & 2 deletions docs/v4/3.XMLBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ To recognize attribute properties group in the JS object so that they can be tri
## 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`
## cdataPropName
To recognize CDATA properties in a JS object so that they can be transformed correcty. This option is supported only if `preserveOrder: true`
## 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.

## format
By default, parsed XML is single line XML string. By `format: true`, you can format it for better view.
Expand Down
2 changes: 1 addition & 1 deletion spec/attr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("XMLParser", function() {
const parser = new XMLParser(options);
let result = parser.parse(xmlData);

console.log(JSON.stringify(result,null,4));
// console.log(JSON.stringify(result,null,4));
// expect(result).toEqual(expected);

result = XMLValidator.validate(xmlData);
Expand Down
4 changes: 2 additions & 2 deletions spec/cdata_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ patronymic</person></root>`;

const options = {
ignoreAttributes: false,
cdataTagName: "__cdata"
cdataPropName: "__cdata"
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
Expand Down Expand Up @@ -297,7 +297,7 @@ patronymic</person></root>`;

const options = {
ignoreAttributes: false,
cdataTagName: "__cdata"
cdataPropName: "__cdata"
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
Expand Down
36 changes: 36 additions & 0 deletions spec/comments_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

const { XMLParser, XMLBuilder, XMLValidator} = require("../src/fxp");

describe("Comments", function() {

it("should parse comment and build them back", function() {
const XMLdata = `
<!--Students grades are uploaded by months-->
<class_list>
<student>
<!--Student details-->
<!--A second comment-->
<name>Tanmay</name>
<grade>A</grade>
</student>
</class_list>`;

const options = {
ignoreAttributes: false,
format: true,
commentPropName: "#comment",
preserveOrder: 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);
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(XMLdata.replace(/\s+/g, ""));
});


});

36 changes: 28 additions & 8 deletions spec/inputType_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,41 @@ describe("XMLParser", function() {
expect(result).toEqual(expected);
});

it("should not parse when invalid value is given", function() {
xit("should not parse when invalid value is given", function() {
const parser = new XMLParser();
const result = parser.parse(23);
// console.log(result)
expect(result).toBeUndefined();
});

xit("should not parse when invalid value is given", function() {
const parser = new XMLParser();
expect(parser.parse(23)).toBeUndefined();
const result = parser.parse([]);
// console.log(result)
expect(result).toBeUndefined();
});

it("should not parse when invalid value is given", function() {
xit("should not parse when invalid value is given", function() {
const parser = new XMLParser( { preserveOrder: true});
const result = parser.parse([]);
expect(result).toBeUndefined();
});

it("should not parse when null", function() {
const parser = new XMLParser( { preserveOrder: true});
expect(parser.parse([])).toBeUndefined();
expect(() => {
parser.parse(null);
// console.log(result);
}).toThrowError("Cannot read property 'toString' of null");
});

it("should not parse when invalid value is given", function() {

const parser = new XMLParser();
expect(parser.parse([])).toBeUndefined();
it("should not parse when undefined", function() {
const parser = new XMLParser( { preserveOrder: true});
expect(() => {
parser.parse();
// console.log(result);
}).toThrowError("Cannot read property 'toString' of undefined");
});


});
6 changes: 3 additions & 3 deletions spec/j2x_ordered_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("XMLBuilder", function() {
const options = {
ignoreAttributes: false,
preserveOrder: true,
cdataTagName: "#CDATA",
cdataPropName: "#CDATA",
allowBooleanAttributes: true,
// format: true,

Expand Down Expand Up @@ -173,7 +173,7 @@ describe("XMLBuilder", function() {
const options = {
preserveOrder: true,
format: true,
// cdataTagName: "#CDATA"
// cdataPropName: "#CDATA"
}
const parser = new XMLParser(options);
let result = parser.parse(XMLdata);
Expand Down Expand Up @@ -208,7 +208,7 @@ describe("XMLBuilder", function() {
const options = {
preserveOrder: true,
format: true,
cdataTagName: "#CDATA"
cdataPropName: "#CDATA"
}
const parser = new XMLParser(options);
let result = parser.parse(XMLdata);
Expand Down
6 changes: 3 additions & 3 deletions spec/j2x_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe("XMLBuilder", function() {
}
};
const builder = new XMLBuilder({
cdataTagName: "__cdata",
cdataPropName: "__cdata",
attributesGroupName: "@",
encodeHTMLchar: true,
format: true,
Expand Down Expand Up @@ -274,7 +274,7 @@ textvalue&gt; <tag>
}
};
const builder = new XMLBuilder({
cdataTagName: "__cdata",
cdataPropName: "__cdata",
attributesGroupName: "@",
encodeHTMLchar: true,
format: true,
Expand Down Expand Up @@ -358,7 +358,7 @@ textvalue&gt; <tag>
attributesGroupName: "$",
textNodeName: "_",
ignoreAttributes: false,
cdataTagName: "$cdata",
cdataPropName: "$cdata",
cdataPositionChar: "\\c",
format: false,
indentBy: "\t",
Expand Down
4 changes: 2 additions & 2 deletions spec/valueProcessors_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe("XMLParser", function() {
const options = {
ignoreAttributes: false,
preserveOrder: true,
cdataTagName: "#CDATA",
cdataPropName: "#CDATA",
tagValueProcessor: (tagName, tagValue, jPath, hasAttributes, isLeafNode) => {
// console.log(tagName, tagValue, jPath, hasAttributes, isLeafNode);
tagValueProcessorCalls.push(`${tagName} ${tagValue} ${jPath} ${hasAttributes} ${isLeafNode}`);
Expand Down Expand Up @@ -301,7 +301,7 @@ describe("XMLParser", function() {
const options = {
ignoreAttributes: false,
preserveOrder: true,
cdataTagName: "#CDATA",
cdataPropName: "#CDATA",
tagValueProcessor: (tagName, tagValue, jPath, hasAttributes, isLeafNode) => {
// console.log(tagName, tagValue, jPath, hasAttributes, isLeafNode);
tagValueProcessorCalls.push(`${tagName} ${tagValue} ${jPath} ${hasAttributes} ${isLeafNode}`);
Expand Down
6 changes: 4 additions & 2 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ type X2jOptions = {
parseTagValue: boolean;
parseAttributeValue: boolean;
trimValues: boolean;
cdataTagName: false | string;
cdataPropName: false | string;
commentPropName: false | string;
tagValueProcessor: (tagName: string, tagValue: string, jPath: string, hasAttributes: boolean, isLeafNode: boolean) => string;
attributeValueProcessor: (attrName: string, attrValue: string, jPath: string) => string;
numberParseOptions: strnumOptions;
Expand All @@ -33,7 +34,8 @@ type XmlBuilderOptions = {
attributesGroupName: false | string;
textNodeName: string;
ignoreAttributes: boolean;
cdataTagName: false | string;
cdataPropName: false | string;
commentPropName: false | string;
format: boolean;
indentBy: string;
arrayNodeName: string;
Expand Down
8 changes: 5 additions & 3 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const defaultOptions = {
attributesGroupName: false,
textNodeName: '#text',
ignoreAttributes: true,
cdataTagName: false,
cdataPropName: false,
format: false,
indentBy: ' ',
suppressEmptyNode: false,
Expand All @@ -18,22 +18,24 @@ const defaultOptions = {
attributeValueProcessor: function(attrName, a) {
return a;
},
preserveOrder: false
preserveOrder: false,
commentPropName: false
};

const props = [
'attributeNamePrefix',
'attributesGroupName',
'textNodeName',
'ignoreAttributes',
'cdataTagName',
'cdataPropName',
'format',
'indentBy',
'suppressEmptyNode',
'tagValueProcessor',
'attributeValueProcessor',
'arrayNodeName', //when array as root
'preserveOrder',
"commentPropName",
// 'rootNodeName', //when jsObject have multiple properties on root level
];

Expand Down
Loading

0 comments on commit 45b912f

Please sign in to comment.