Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating case-sensitive or 'camelCase' XML nodes using JavaScript or jQuery

I need to create some case-sensitive XML nodes in JavaScript which have attribute names with a colon in between them.

Example: <Data ss:Type="String">Contact Name</Data>

When I try to create an element via JavaScript with the createElement() function it creates it in lowercase. Also, there are a lot of problems adding the attributes with a colon in them. For example: ss:Type="String"

This is Excel data, and I am saving the the source .xml file back to an .xls file. The case of the XML elements and attributes is very important for Excel to be able to read the file.

Any examples or pointers will be great help.

like image 351
Anmol Saraf Avatar asked Nov 20 '25 12:11

Anmol Saraf


1 Answers

The JavaScript methods you are looking for are document.createElementNS and elm.setAttributeNS

var e = document.createElementNS('http://www.microsoft.com/office/excel/', 'Data');
e.setAttributeNS('http://www.microsoft.com/office/excel/', 'ss:Type', 'String');
e.textContent = 'Contact Name';

Then if you want to get strings back, .innerHTML won't work anymore so you have to use an XMLSerializer

var s = new XMLSerializer();
s.serializeToString(e);
like image 54
Paul S. Avatar answered Nov 23 '25 01:11

Paul S.