MMR
MMR

Reputation: 3029

How to add contents to an xml file from js

When i do this

   var obj = {item:{title: params.title,description: text, link: req.headers['origin']+"/blog/blog-description/"+params.blog_id,guid:req.headers['origin']+"/blog/blog-description/"+params.blog_id}};

i got,

     <item>
      <title>errerewr</title>
        <description>erewrewrewrewrw</description>
          <link>
            http://localhost:4220/blog//ererewrewrqwrwe
          </link>
           <guid>
            http://localhost:4220/blog/ererewrewrqwrwe
           </guid>
     </item>

but i want to add enclosure like below,

 <enclosure url="http://example.com/file.mp3" length="123456789" type="audio/mpeg" />

so far i had done for adding cotents in between tags but now for enclouse i had to add contents in the tag here i got strucked.Can anyone suggest help.

Upvotes: 0

Views: 501

Answers (1)

SergeyK
SergeyK

Reputation: 1577

For most js-2-xml conversion utils you should use '@' or '$' for adding attributes, while convertng js object to xml.

Example:

 var obj = {
    item:{
        title: params.title,
        description: text,
        link: req.headers['origin']+"/blog/blog-description/"+params.blog_id,guid:req.headers['origin']+"/blog/blog-description/"+params.blog_id,
        enclosure: {
                '@' : {
                    url: params.url, //you need to replace it aacording to your app logic of course
                    length: params.length,
                    type: params.type
                }
            }
    }
};

Upvotes: 2

Related Questions