2

I am trying to send an xml file as an attachment in an email.

I have a json from which I will create an xml. My question is, can I just write a string of tags into a file and is it a valid xml file?

I am thinking about using template literals(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) like

  const myXmlString =   `<ENVELOPE>
    <HEADER>
        <TALLYREQUEST>${myVarsValue}</TALLYREQUEST>
    </HEADER>
    <BODY></BODY>
</HEADER>
</ENVELOPE>`

And write a file

 file.writeFileSync("./filesx.xml", myXmlString, function (err) {
  if (err) throw err;
});

Is this still valid xml file?

I am trying to use this https://www.npmjs.com/package/xmlbuilder which generated xml like this:

   var xml = builder.create('root')
  .ele('xmlbuilder')
    .ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
  .end({ pretty: true});

But I don't know how to write this into a file.

Can I just use the first method or should I use the xmlbuilder? Even if the first method is ok, can someone tell me how I an write its result to a file?

I tried this https://github.com/oozcitak/xmlbuilder-js/issues/227 but I get an error saying

    <?xml version="1.0"?><root><example0/><example1/><example2/><example3/><example4/><example5/><example6/><example7/><example8/><example9/></root>
TypeError: Cannot read property 'match' of undefined
    at XMLStringifier.assertLegalChar (/Volumes/D/Vue admin node/card91_nodejs_boilerplate/node_modules/xmlbuilder/lib/XMLStringifier.js:209:32)
    at XMLElement.element (/Volumes/D/Vue admin node/card91_nodejs_boilerplate/node_modules/xmlbuilder/lib/XMLNode.js:127:25

1 Answer 1

1

Question 1

Can I just write a string of tags into a file and is it a valid xml file?

Yes, it is as long as your XML content is valid. In your example, you may need to add XML prolog <?xml version="1.0" encoding="UTF-8"?> to the beginning of your XML string. However, it is optional.

Question 2

Can I just use the first method or should I use the xmlbuilder? Even if the first method is ok, can someone tell me how I an write its result to a file?

You should use xmlbuilder if the XML content is complex. When you call end() function your document is converted into a string. Therefore, you can use the write to file method in the first question to create the XML file.

var xml = builder.create('root')
    .ele('xmlbuilder')
        .ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
    .end({ pretty: true});

file.writeFileSync("./filesx.xml", xml, function (err) {
    if (err) throw err;
});

You can read more about xmlbuilder functions here.
https://github.com/oozcitak/xmlbuilder-js/wiki

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

Comments

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.