Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ var options = {
arrayMode: false, //"strict"
attrValueProcessor: (val, attrName) => he.decode(val, {isAttributeValue: true}),//default is a=>a
tagValueProcessor : (val, tagName) => he.decode(val), //default is a=>a
stopNodes: ["parse-me-as-string"]
stopNodes: ["parse-me-as-string"],
alwaysCreateTextNode: false
};

if( parser.validate(xmlData) === true) { //optional (it'll return an object in case it's not valid)
Expand Down Expand Up @@ -201,7 +202,7 @@ Validator returns the following object in case of error;
* **tagValueProcessor** : Process tag value during transformation. Like HTML decoding, word capitalization, etc. Applicable in case of string only.
* **attrValueProcessor** : Process attribute value during transformation. Like HTML decoding, word capitalization, etc. Applicable in case of string only.
* **stopNodes** : an array of tag names which are not required to be parsed. Instead their values are parsed as string.

* **alwaysCreateTextNode** : When `true`, forces the parser always return a property for the `textNodeName` even if there are no attributes or node children.
</details>

<details>
Expand Down
25 changes: 25 additions & 0 deletions spec/xmlParser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,4 +885,29 @@ describe("XMLParser", function() {
});
expect(result).toEqual(expected);
});

it("should create text node even if there are no attributes or node children", function() {
const xmlData = `<rootNode>
<tag>value</tag>
<tag2 some="attribute">12345</tag2>
</rootNode>`;
const expected = {
"rootNode": {
"tag": {
"#text": "value"
},
"tag2": {
"#text": 12345,
"@_some": "attribute"
},
}
};

const result = parser.parse(xmlData, {
alwaysCreateTextNode : true,
ignoreAttributes: false,
parseAttributeValue: true
});
expect(result).toEqual(expected);
});
});
2 changes: 1 addition & 1 deletion src/node2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const convertToJson = function(node, options, parentTagName) {
const jObj = {};

// when no child node or attr is present
if ((!node.child || util.isEmptyObject(node.child)) && (!node.attrsMap || util.isEmptyObject(node.attrsMap))) {
if (!options.alwaysCreateTextNode && (!node.child || util.isEmptyObject(node.child)) && (!node.attrsMap || util.isEmptyObject(node.attrsMap))) {
return util.isExist(node.val) ? node.val : '';
}

Expand Down
1 change: 1 addition & 0 deletions src/parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type X2jOptions = {
tagValueProcessor: (tagValue: string, tagName: string) => string;
attrValueProcessor: (attrValue: string, attrName: string) => string;
stopNodes: string[];
alwaysCreateTextNode: boolean;
};
type strnumOptions = {
hex: boolean;
Expand Down
6 changes: 4 additions & 2 deletions src/xmlstr2xmlnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const defaultOptions = {
attrValueProcessor: function(a, attrName) {
return a;
},
stopNodes: []
stopNodes: [],
alwaysCreateTextNode: false
//decodeStrict: false,
};

Expand All @@ -67,7 +68,8 @@ const props = [
'attrValueProcessor',
'parseTrueNumberOnly',
'numParseOptions',
'stopNodes'
'stopNodes',
'alwaysCreateTextNode'
];
exports.props = props;

Expand Down