Skip to content

Commit

Permalink
replace attrsMap with attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Nov 19, 2021
1 parent 3de998a commit 4d3b8be
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
7 changes: 5 additions & 2 deletions docs/v4/2.XMLparseOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ When `preserveOrder: true` , you get some long ugly result. That's because this
```js
const XMLdata = `
<!--Students grades are uploaded by months-->
<class_list>
<class_list standard="3">
<student>
<!--Student details-->
<!--A second comment-->
Expand Down Expand Up @@ -495,7 +495,10 @@ const XMLdata = `
}
]
}
]
],
"attributes": {
"standard" : "3"
}
}
]
```
Expand Down
21 changes: 12 additions & 9 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function buildAttributesMap(attrStr, jPath, options) {
}

const parseToOrderedJsObj = function(xmlData, options) {
xmlData = xmlData.replace(/\r\n?/g, "\n");
xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
const xmlObj = new xmlNode('!xml');
let currentNode = xmlObj;
let textData = "";
Expand Down Expand Up @@ -170,7 +170,7 @@ const parseToOrderedJsObj = function(xmlData, options) {
, currentNode.tagname
, jPath
,false
, currentNode.attrsMap ? Object.keys(currentNode.attrsMap).length !== 0 : false
, currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
, Object.keys(currentNode.child).length === 0);
if(textData !== undefined && textData !== "") currentNode.add(options.textNodeName, textData);
textData = "";
Expand Down Expand Up @@ -201,7 +201,7 @@ const parseToOrderedJsObj = function(xmlData, options) {
, currentNode.tagname
, jPath
,false
, currentNode.attrsMap ? Object.keys(currentNode.attrsMap).length !== 0 : false
, currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
, Object.keys(currentNode.child).length === 0);

if(textData !== undefined && textData !== "") currentNode.add(options.textNodeName, textData);
Expand All @@ -228,7 +228,7 @@ const parseToOrderedJsObj = function(xmlData, options) {
, currentNode.tagname
, jPath
,false
, currentNode.attrsMap ? Object.keys(currentNode.attrsMap).length !== 0 : false
, currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
, Object.keys(currentNode.child).length === 0);

if(textData !== undefined && textData !== "") currentNode.add(options.textNodeName, textData);
Expand Down Expand Up @@ -276,7 +276,7 @@ const parseToOrderedJsObj = function(xmlData, options) {
, currentNode.tagname
, jPath
, false
, currentNode.attrsMap ? Object.keys(currentNode.attrsMap).length !== 0 : false
, currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
, false);
if(textData !== undefined && textData !== "") currentNode.add(options.textNodeName, textData);
textData = "";
Expand All @@ -287,7 +287,8 @@ const parseToOrderedJsObj = function(xmlData, options) {
jPath += jPath ? "." + tagName : tagName;
}

if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){//selfClosing tag
//selfClosing tag
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){

if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
tagName = tagName.substr(0, tagName.length - 1);
Expand All @@ -298,20 +299,22 @@ const parseToOrderedJsObj = function(xmlData, options) {

const childNode = new xmlNode(tagName);
if(tagName !== tagExp && shouldBuildAttributesMap){
childNode.attrsMap = buildAttributesMap(tagExp, jPath , options);
childNode.attributes = buildAttributesMap(tagExp, jPath , options);
}
jPath = jPath.substr(0, jPath.lastIndexOf("."));
// tagsNodeStack.push(currentNode);
currentNode.addChild(childNode);
}else{//opening tag
}
//opening tag
else{

const childNode = new xmlNode( tagName);
tagsNodeStack.push(currentNode);

childNode.startIndex=closeIndex; //for further processing

if(tagName !== tagExp && shouldBuildAttributesMap){
childNode.attrsMap = buildAttributesMap(tagExp, jPath, options);
childNode.attributes = buildAttributesMap(tagExp, jPath, options);
}
currentNode.addChild(childNode);
currentNode = childNode;
Expand Down
6 changes: 3 additions & 3 deletions src/xmlparser/xmlNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class XmlNode{
constructor(tagname) {
this.tagname = tagname;
this.child = []; //nested tags, text, cdata, comments in order
this.attrsMap = {}; //attributes map
this.attributes = {}; //attributes map
}
add(key,val){
// this.child.push( {name : key, val: val, isCdata: isCdata });
this.child.push( {[key]: val });
}
addChild(node) {
if(node.attrsMap && Object.keys(node.attrsMap).length > 0){
this.child.push( { [node.tagname]: node.child, attributes: node.attrsMap });
if(node.attributes && Object.keys(node.attributes).length > 0){
this.child.push( { [node.tagname]: node.child, attributes: node.attributes });
}else{
this.child.push( { [node.tagname]: node.child });
}
Expand Down

0 comments on commit 4d3b8be

Please sign in to comment.