CHunky Universe of Vigourous Astonishing SHarepoint :)

javascript: serialize as xml

Why should we need to serialize javascript objects as XML. I don’t know. It is of course more a server side need. And there actually a need for javascript serialization as xml in node.js.

To serialize as json is very simple: JSON.stringify(myobjects). I was just curious if there was a tool for xml serialization in javascript. There is a nice javascript tool called XMLWriter, developed by Ariel Flesler. Consider you have articles and want to serialize them as xml, let’s create a function for serializing:

function createProductXML(articles) {
  var v = new XMLWriter( 'UTF-8', '1.0' );
  v.indentChar = "  ";

  for(var a in articles) {
    var article = articles[a];
    v.writeElementString('number', article.number);
    v.writeElementString('description', article.description);
    v.writeElementString('price', article.price.toString());
    v.writeElementString('quantity', article.quantity.toString());

    for(var c in article.categories) {
      v.writeElementString('category', article.categories[c]);

  return v.flush();

And now we can test it:

var articles = [];
  name: "Ski", 
  number: "X21", 
  description: "The best ski for beginners", 
  summercollection: false, 
  wintercollection: true, 
  price: 20.5, 
  quantity: 20, 
  categories: ["winter", "snow", "fun", "beginner"]
  name: "Helicopter", 
  number: "T21", 
  description: "The best helicopter toy ever", 
  summercollection: true, 
  wintercollection: false, 
  price: 40, 
  quantity: 10, 
  categories: ["toy", "fly", "fun", "children"]
  name: "Book", 
  number: "B21", 
  description: "The best book ever", 
  summercollection: true, 
  wintercollection: true, 
  price: 100, 
  quantity: 5, 
  categories: ["reading", "education", "fun", "puzzle"]
var xml = createProductXML(articles);

The resulting xml is:

<?xml version="1.0" encoding="UTF-8" ?>
  <article summercollection="false" wintercollection="true">
    <description>The best ski for beginners</description>
  <article summercollection="true" wintercollection="false">
    <description>The best helicopter toy ever</description>
  <article summercollection="true" wintercollection="true">
    <description>The best book ever</description>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Coder, Speaker, Author

Office 365, SharePoint and Azure

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

Paul J. Swider - RealActivity

RealActivity is a specialized healthcare services and solution advisory firm.

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders


Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

Toronto Blockchain Consulting and Smart Contract Development


SharePoint for everyone


Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint


Mostly what I know about SharePoint - CommunicoCuspis

%d bloggers like this: