CHUVASH.eu

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 = "  ";
  v.writeStartDocument();
  v.writeStartElement('articles');

  for(var a in articles) {
    var article = articles[a];
    v.writeStartElement('article');
    v.writeAttributeString('summercollection',article.summercollection.toString());
    v.writeAttributeString('wintercollection',article.wintercollection.toString());
    v.writeElementString('name', article.name);
    v.writeElementString('number', article.number);
    v.writeElementString('description', article.description);
    v.writeElementString('price', article.price.toString());
    v.writeElementString('quantity', article.quantity.toString());
    v.writeStartElement('categories');

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

  v.writeEndDocument();
  return v.flush();
}

And now we can test it:

var articles = [];
articles.push({
  name: "Ski", 
  number: "X21", 
  description: "The best ski for beginners", 
  summercollection: false, 
  wintercollection: true, 
  price: 20.5, 
  quantity: 20, 
  categories: ["winter", "snow", "fun", "beginner"]
});
articles.push({
  name: "Helicopter", 
  number: "T21", 
  description: "The best helicopter toy ever", 
  summercollection: true, 
  wintercollection: false, 
  price: 40, 
  quantity: 10, 
  categories: ["toy", "fly", "fun", "children"]
});
articles.push({
  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);
console.log(xml);

The resulting xml is:

<?xml version="1.0" encoding="UTF-8" ?>
<articles>
  <article summercollection="false" wintercollection="true">
    <name>Ski</name>
    <number>X21</number>
    <description>The best ski for beginners</description>
    <price>20.5</price>
    <quantity>20</quantity>
    <categories>
      <category>winter</category>
      <category>snow</category>
      <category>fun</category>
      <category>beginner</category>
    </categories>
  </article>
  <article summercollection="true" wintercollection="false">
    <name>Helicopter</name>
    <number>T21</number>
    <description>The best helicopter toy ever</description>
    <price>40</price>
    <quantity>10</quantity>
    <categories>
      <category>toy</category>
      <category>fly</category>
      <category>fun</category>
      <category>children</category>
    </categories>
  </article>
  <article summercollection="true" wintercollection="true">
    <name>Book</name>
    <number>B21</number>
    <description>The best book ever</description>
    <price>100</price>
    <quantity>5</quantity>
    <categories>
      <category>reading</category>
      <category>education</category>
      <category>fun</category>
      <category>puzzle</category>
    </categories>
  </article>
</articles>
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 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

paul.tavares

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

| Blockchain | Serverless Architecture | Microservices Architecture | DevOps | AWS Lambda | Teraform |

SPJoel

SharePoint for everyone

SharePointRyan

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

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

%d bloggers like this: