need to create xml using js

V

vish

Hello,


i have a response from a API which returns to me different attributes.
i need to write a js code to convert that response into an xml file,

can someone please help me to get started..? i am new to js

the response is like below.

username=ghijkl&token=l-2-AAABIL-
AKomsEyC_JbEe57iIkphI7f99_iWipH5aRc7bQMaIUcEFhBVL4lLuXvpzpGL7vl6xBzTsQCEUPZja1V2KpSr_XS2e_q6pz2gfCzA.&token_secret=xxc3bucsmp3ujbjykcf1ce&expires_in=86399

need to convert above into an xml

like below


<?xml version="1.0" encoding="UTF-8"?>
<root>
<z0>ghijkl</z0>
<z1>l-2-AAABIL-
AKomsEyC_JbEe57iIkphI7f99_iWipH5aRc7bQMaIUcEFhBVL4lLuXvpzpGL7vl6xBzTsQCEUPZja1V2KpSr_XS2e_q6pz2gfCzA.</
z1>
<z2>bucsfdf3mp3ujbkcf1ce</z2>
<z3>86399</z3>
</root>

any help is appreciated.

thanks in advance

vish
 
T

Tad J McClellan

vish said:
i need to write a js code to convert that response into an xml file,
like below


<?xml version="1.0" encoding="UTF-8"?>
<root>
<z0>ghijkl</z0>
<z1>l-2-AAABIL-
AKomsEyC_JbEe57iIkphI7f99_iWipH5aRc7bQMaIUcEFhBVL4lLuXvpzpGL7vl6xBzTsQCEUPZja1V2KpSr_XS2e_q6pz2gfCzA.</
z1>
<z2>bucsfdf3mp3ujbkcf1ce</z2>
<z3>86399</z3>
</root>


var map = {
'username': 'z0',
'token': 'z1',
'token_secret': 'z2',
'expires_in': 'z3',
};
var str = 'username=ghijkl&token=l-2-AAABIL-AKomsEyC_JbEe57iIkphI7f99_'
+ 'iWipH5aRc7bQMaIUcEFhBVL4lLuXvpzpGL7vl6xBzTsQCEUPZja1V2KpSr_'
+ 'XS2e_q6pz2gfCzA.&token_secret=xxc3bucsmp3ujbjykcf1ce&expires_in=86399'

var xml = '<?xml version="1.0" encoding="UTF-8"?>\n<root>\n';

var pairs = str.split(/&/);
for (var i=0; i<pairs.length; i++) {
var pair = pairs.split(/=/);
var tag = map[pair[0]];
xml += ' <' + tag + '>' + pair[1] + '</' + tag + '>\n';
}
xml += '</root>\n';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,245
Latest member
ShannonEat

Latest Threads

Top