Skip to content
Closed
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
15 changes: 15 additions & 0 deletions spec/xmlParser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,21 @@ describe("XMLParser", function() {
expect(result).toEqual(expected);
});


it("should enforce text node when flag supplied", function() {
const xmlData = `<tag.1 t1='1'>val1</tag.1><tag.2>val2</tag.2>`;
const expected = {
"tag.1": { "#text": 'val1', "@_t1": '1' },
"tag.2": { "#text": 'val2' },
};

const result = parser.parse(xmlData, {
ignoreAttributes: false,
forceTextNode: true,
});
expect(result).toEqual(expected);
});

it("should not parse text value with tag", function() {
const xmlData = `<score><c1>71<message>23</message>29</c1></score>`;
const expected = {
Expand Down
2 changes: 2 additions & 0 deletions src/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const buildOptions = require('./util').buildOptions;

const defaultOptions = {
attributeNamePrefix: '@_',
forceTextNode: false,
attrNodeName: false,
textNodeName: '#text',
ignoreAttributes: true,
Expand All @@ -22,6 +23,7 @@ const defaultOptions = {

const props = [
'attributeNamePrefix',
'forceTextNode',
'attrNodeName',
'textNodeName',
'ignoreAttributes',
Expand Down
8 changes: 8 additions & 0 deletions src/node2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ const util = require('./util');
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.forceTextNode) {
const asArray = util.isTagNameInArrayMode(node.tagname, options.arrayMode, parentTagName)
jObj[options.textNodeName] = asArray ? [node.val] : node.val;
return jObj;
}

return util.isExist(node.val) ? node.val : '';
}

Expand Down
2 changes: 2 additions & 0 deletions src/parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ type X2jOptions = {
attrNodeName: false | string;
textNodeName: string;
ignoreAttributes: boolean;
forceTextNode: boolean;
ignoreNameSpace: boolean;
allowBooleanAttributes: boolean;
parseNodeValue: boolean;
Expand All @@ -23,6 +24,7 @@ type validationOptions = {
type validationOptionsOptional = Partial<validationOptions>;
type J2xOptions = {
attributeNamePrefix: string;
forceTextNode: boolean;
attrNodeName: false | string;
textNodeName: string;
ignoreAttributes: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/xmlstr2xmlnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const defaultOptions = {
attrNodeName: false,
textNodeName: '#text',
ignoreAttributes: true,
forceTextNode: false,
ignoreNameSpace: false,
allowBooleanAttributes: false, //a tag can have attributes without any value
//ignoreRootElement : false,
Expand All @@ -48,6 +49,7 @@ const props = [
'attributeNamePrefix',
'attrNodeName',
'textNodeName',
'forceTextNode',
'ignoreAttributes',
'ignoreNameSpace',
'allowBooleanAttributes',
Expand Down