1

I'm creating an XML file using a foreach to add the data.

Problem:

I need to create an ELEMENT < Listings > and inside it I need to put the children that will be Listing given .... < /Listing > and after that FOR I need to close the < /Listings >

What is happening is that it is creating the xml file with the Listings element already closed example: < Listings/ >

Code:

var xml = builder.create('ListingDataFeed', { version: '1.0', encoding: "UTF-8"});

    xml.ele('Listings');

    // var listings = xml.ele('Listings');
    for(var i=0; i< result.length; i++){

        xml.ele('Listing')

        .ele('ListingID', `${result[i].sku}`).up()
        .ele('Title').dat(`${result[i].name}`).up()
        .ele('TransactionType', `${transaction}`).up()
        .ele('PublicationType', `${type_offer}`).up()
        .ele('DetailViewUrl', 'https://sortinvestimentos.com.br/imovel/' + `${result[i].url}`).up()
        .ele('Media')
            .ele('Item', {'medium': 'image'}, `${result[i].media_1}`).up()
        .up() // Media finish
        .ele('ContactInfo')
            .ele('Name', 'Sort Investimentos').up()
            .ele('Email', '[email protected]').up()
        .up()
    .up() // Listing finish
    }
    .up(); // Listings finish

    var root = xml.end({ pretty: true});

This result

enter image description here

4
  • let listings = xml.ele('Listing'); for(...) { Listings.ele('Listing')..}.. Commented Jul 13, 2022 at 21:19
  • And where do I put it after Commented Jul 14, 2022 at 2:32
  • What I mean is that when you add your "listing" element, you should append it to the "Listings" element, not to the "xml" element which is the root. So you assign the listings element to a variable, than you can append to it inside the loop. That's how it is shown in the docs: oozcitak.github.io/xmlbuilder2/node-creation-functions.html#ele Commented Jul 14, 2022 at 17:46
  • Can you be more detailed? Answer with the code mentioned in the middle of the question code please Commented Jul 15, 2022 at 18:16

1 Answer 1

2

Here is a working example:

var {create} = require("xmlbuilder2");
const R = [{name: 'foo1', sku: 'bar1'}, {name:'foo2', sku: 'bar2'}];
let xml = create({ version: '1.0', encoding: "UTF-8"})
    .ele('ListingDataFeed')
    .ele('Listings');
for(let i = 0; i<R.length;i++){
 xml.ele("Listing")
    .ele("listingId: ", `${R[i].sku}`).up()
    .ele('Title').dat(`${R[i].name}`).up().up();
        //rest of your code
}

The point being that the ele() function creates a new node and returns that node. Therefore, when you call your loop, you have to make sure that the xml variable is pointing to the Listings node, so the child elements are attached to it.
In the code above, at the beginning of the loop, the xml var is pointing the newly created Listings element.

Sign up to request clarification or add additional context in comments.

1 Comment

I go to execute this code, after i'm back to say the result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.