0

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.

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98

1 Answers1

0

Can you provide an example of how the XML is outputted now in your code and how you want it to be? Because it is not really clear for me what the exact problem is. You want to append but also you don't :-)

Update: I think I understand the problem more clearly. I suggest creating a helper function which generates an XML root object. When submitting the value, you can then 'reset' the XMLBuilder object by creating an empty new one.

var builder = require("xmlbuilder");

function setupXML(currentTime) {
  var root = builder.create("Header");
  root.att(currentTime);
  return root;
}

function createXMLData(xmlRoot, query) {
  xmlRoot.ele(query.time);
  xml = xmlRoot.end({ pretty: true });
};

var items = [
  {
    time: "time1",
  }
];

var currentTime = 'value';
var root = setupXML(currentTime);

items.forEach((item) => {
  createXMLData(root, item);
});
Robbe Claessens
  • 79
  • 1
  • 1
  • 6
  • Thanks for the response. I have added the XML samples. I hope that helps. – BATMAN_2008 Aug 20 '20 at 07:59
  • I would like to append for each element of the Array. I don't want the `root` element to append, I want to do it on my own because if `root` element is doing then its keeping track of the previous values and appending with that also so if I am doing then I can clear the previous values and append-only the fresh XML values. On the other side, I am fine if the `root` element is doing the appending but in this case, I would like to empty the previous values of `root` which I am unable to do. – BATMAN_2008 Aug 20 '20 at 08:04
  • So essentially, you want to create 2 different XML 'documents'. The first time you click on the submit you add the headers. But then when you click again, you get the previous values and the new values in the new XML. So you want to reset the initial state when submitting the button? Is that correct? – Robbe Claessens Aug 20 '20 at 14:24
  • Yes exactly. I would like to reset the `root` element value of the XML whenever I click on the submit button and call the function `createXMLData`. Now the problem I am facing is that if I initialize the `root` within the function then XML header would be created for each element in the array which I don't want. I want to have single header and all the other elements should be appended to it. I hope I was able to explain to you the problem clearly. – BATMAN_2008 Aug 20 '20 at 15:01
  • Thanks for the response. How are you resetting the `root` value? Because if I try to reset using the `root = ' ' ´ then I get the error. – BATMAN_2008 Aug 23 '20 at 12:50