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
36 changes: 36 additions & 0 deletions spec/transform_tagname_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

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

describe("XMLParser", function() {
it("should parse lowercase tagnames", function() {
const xmlData = `<?xml version='1.0'?>
<root>
<person>Person 1</person>
<Person>Person 2</Person>
<PERSON>Person 3</PERSON>
<person>Person 4</person>
</root>
`;

const parser = new XMLParser({
// transformTagName: (tagName) => tagName
ignoreDeclaration: true,
transformTagName: (tagName) => tagName.toLowerCase()
});
let result = parser.parse(xmlData);

const expected = {
"root": {
"person": [
"Person 1",
"Person 2",
"Person 3",
"Person 4"
]
}
};

expect(result).toEqual(expected);
});
});
1 change: 1 addition & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type X2jOptions = {
htmlEntities: boolean;
ignoreDeclaration: boolean;
ignorePiTags: boolean;
transformTagName: ((tagName: string) => string) | false;
};
type strnumOptions = {
hex: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const defaultOptions = {
{ regex: new RegExp("\"", "g"), val: "&quot;" }
],
processEntities: true,
stopNodes: []
stopNodes: [],
transformTagName: false,
};

function Builder(options) {
Expand Down
3 changes: 2 additions & 1 deletion src/xmlparser/OptionsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const defaultOptions = {
processEntities: true,
htmlEntities: false,
ignoreDeclaration: false,
ignorePiTags: false
ignorePiTags: false,
transformTagName: false,
};

const buildOptions = function(options) {
Expand Down
13 changes: 12 additions & 1 deletion src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ const parseXml = function(xmlData) {
}
}

if(this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

if(currentNode){
textData = this.saveTextToParentTag(textData, currentNode, jPath);
}
Expand Down Expand Up @@ -257,12 +261,15 @@ const parseXml = function(xmlData) {

i = closeIndex + 2;
}else {//Opening tag

let result = readTagExp(xmlData,i, this. options.removeNSPrefix);
let tagName= result.tagName;
let tagExp = result.tagExp;
let attrExpPresent = result.attrExpPresent;
let closeIndex = result.closeIndex;

if (this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

//save text as child node
if (currentNode && textData) {
Expand Down Expand Up @@ -322,6 +329,10 @@ const parseXml = function(xmlData) {
}else{
tagExp = tagExp.substr(0, tagExp.length - 1);
}

if(this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

const childNode = new xmlNode(tagName);
if(tagName !== tagExp && attrExpPresent){
Expand Down