4

My xml file

   <?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
      <title>XpertDox Home Page</title>
  <link>http://www.xpertdox.com</link>
    <description>Find a doctor</description>
<item>
<title>Xpertdox</title>
<link>http://www.xpertdox.com/rss.xml</link>
<description>Find an Xpert Doctor for an unknown Disease</description>
</item>

  I want  to  add this
    <item>
<title>Tony</title>
<link>startk</link>
<description>hi</description>
</item>

my node.js

var obj = {title: "Tony", link: "Stark" , "description":"hi"};
var fs = require('fs');
var xml2js = require('xml2js');

var builder = new xml2js.Builder();
var xml = builder.buildObject(obj);  

fs.writeFile('feed.xml', xml, function (err){
if (err) throw err;
    console.log('It\'s saved!');
}); 

I want to replace my title tag and link as declared in my obj but this is not working.Can any one please change my code........

4
  • Could you specify the error/unexpected behaviour you are getting? Commented Aug 4, 2016 at 11:21
  • Did I understood you correctly, you want to read xml from a file, replace title and link values in it, and write it back to the file? Commented Aug 4, 2016 at 11:24
  • Hi deepdownunder,i am getting the error as permission denied opening file. Commented Aug 4, 2016 at 11:30
  • When i create a new object it is overwritting the old one Commented Aug 4, 2016 at 11:52

3 Answers 3

3

Bumped into this post on google, I was looking for something slightly different, but here, this code of mine should solve this problem for anyone else who comes across this post.

function xmlFileToObject(path) {
    return new Promise((resolve,reject) => {
        getFile(path).then(xml => {
            parseXml(xml).then(result => {
                resolve(result);
            });
        });
    });
}
function getFile(path) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, 'utf8', function (err,data) {
            if (err) console.error(err);
            resolve(data);
        });
    });
}
function parseXml(xml) {
    return new Promise((resolve,reject) => {
        var parseString = require('xml2js').parseString;
        parseString(xml, function (err, result) {
            resolve(result);
        });
    });
}
Sign up to request clarification or add additional context in comments.

Comments

2

Thanks Shard. Just more E6 convention: `

const getFile = dir => new Promise((resolve, reject) => {
  fs.readFile(dir, 'utf8', (err, data) =>
    (err ? reject(err) : resolve(data)));
});

const parseXml = xml => new Promise((resolve, reject) => {
  require('xml2js').parseString(xml, (err, result) =>
    (err ? reject(err) : resolve(result)));
});

const xmlFileToObject = dir => getFile(dir)
  .then(xml => parseXml(xml))
  .then(result => console.log(result))
  .catch(e => console.error(e));

`

Comments

0
var builder = require('xmlbuilder');
var doc = builder.create('root');

doc.ele('xmlbuilder')
    .att('for', 'node-js')
    .ele('repo')
      .att('type', 'git')
      .txt('git://github.com/oozcitak/xmlbuilder-js.git') 
    .up()
  .up()
  .ele('test')
    .txt('complete');

console.log(doc.toString({ pretty: true }));

1 Comment

Please give a little description of your code.

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.