I tried searching the answer but as there a limited number of questions on XMLBUILDER could not find. Hence posting the question really sorry if found duplicate.
I am using the XMLBUILDER
Node.js module to create the XML for my project. I have a function which would create the XML for me but due to some reason, the root
element is always storing the previous value due to which every time the XML would be appended with previous values and I get it twice.
Here is the Node.js controller which would create XML:
var root = builder.create('Header')
root.att(currentTime)
exports.createXMLData = function(Query,callback){
var input = Query.input;
var OuterTag = root.ele(input.Type)
//Add the time to XML
OuterTag.ele('time', input.EventTime).up()
//Add the reason to the XML
OuterTag.ele('reason',input.ErrorReason)
//Like this I have many more tags
xml = root.end({ pretty: true});
callback(xml);
}
I have an Array which has the set of values and I need to create single XML which would append all the values so I am placing the root
outside because I do not want the header to be added every time. For every element in the Array, I would call the function createXMLData
so my required XML can be created and I can append myself to the XML but every time I call this function it would keep track of the previous value and I would get new values along with previous values.
For Example: If I have 2 elements in the array and click on SUBMIT button to create XML then at first it creates the XML correctly with 2 elements but when I click on it again then it would create the XML again along with previous values so I would get 4 elements.
I know if place the root
tag within the function then I would get the Header twice for each element in the array which I do not want. I want to have only 1 header for the whole XML. I tried emptying the root element using the root = ""
within the function but it throws some error.
I am sure its some small thing that I am missing but I am unable to figure it out. Can someone please help me with this?
So for the first time when I create then I would get something like this XML:
<?xml version="1.0"?>
<header>
<time> 2020-08-20T09:35:25+02:00 </time>
<reason> dummyReason </reason>
<time> 2020-08-20T09:35:30+02:00 </time>
<reason> dummyReason </reason>
</header>
If I create again by clicking on Submit button then I would get something like this:
<?xml version="1.0"?>
<header>
<time> 2020-08-20T09:35:25+02:00 </time>
<reason> dummyReason </reason>
<time> 2020-08-20T09:35:30+02:00 </time>
<reason> dummyReason </reason>
<time> 2020-08-20T09:36:25+02:00 </time>
<reason> dummyReason </reason>
<time> 2020-08-20T09:36:30+02:00 </time>
<reason> dummyReason </reason>
</header>
Note that I am not appending the elements every time when I call for the array elements. This has been done automatically by the root
element.