Friday, October 5, 2012

XML2JSON – Command line tool for converting XML to JSON

XML2JSON is a simple command line tool for converting XML files to JSON. It uses the JSON.NET library.

Usage:

XML2JSON.exe input.xml output.json


Example Input:



<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>


Example Output:



{
"catalog": {
"book": [
{
"id": "bk101",
"author": "Gambardella, Matthew",
"title": "XML Developer's Guide",
"genre": "Computer",
"price": "44.95",
"publish_date": "2000-10-01",
"description": "An in-depth look at creating applications \r\n with XML."
},
{
"id": "bk102",
"author": "Ralls, Kim",
"title": "Midnight Rain",
"genre": "Fantasy",
"price": "5.95",
"publish_date": "2000-12-16",
"description": "A former architect battles corporate zombies, \r\n an evil sorceress, and her own childhood to become queen \r\n of the world."
}
]
}
}


Code


This is the code for the program:





using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using Newtonsoft.Json;
using Formatting = Newtonsoft.Json.Formatting;
namespace XML2JSON
{
class Program
{
static void Main(string[] args)
{
var inputXml = args[0];
var outputJson = args[1];
var doc = new XmlDocument();
doc.Load(inputXml);
//strip comments from xml
var comments = doc.SelectNodes("//comment()");
if (comments != null)
{
foreach (var node in comments.Cast<XmlNode>())
{
if (node.ParentNode != null)
node.ParentNode.RemoveChild(node);
}
}
var rawJsonText = JsonConvert.SerializeXmlNode(doc.DocumentElement, Formatting.Indented);
//strip the @ and # characters
var strippedJsonText = Regex.Replace(rawJsonText, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase);
strippedJsonText = Regex.Replace(strippedJsonText, "(?<=\")(#)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase);
//save out
using (var outputFileStream = File.Open(outputJson, FileMode.Create, FileAccess.Write))
{
using (var outputStreamWriter = new StreamWriter(outputFileStream))
{
outputStreamWriter.Write(strippedJsonText);
outputStreamWriter.Flush();
outputStreamWriter.Close();
}
}
}
}
}
view raw Program.cs hosted with ❤ by GitHub






The full .NET solution is available here: https://github.com/SneakyBrian/XML2JSON

No comments:

Post a Comment