diff --git a/.gitignore b/.gitignore index 1bd7226..24211de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules *.swp +*.DS_Store diff --git a/README.md b/README.md index d7c6b1c..1ec3f09 100644 --- a/README.md +++ b/README.md @@ -2,31 +2,71 @@ It does not parse the following elements: -* CDATA sections +* CDATA sections (*) * Processing instructions * XML declarations * Entity declarations * Comments -## Installation -`npm install xml2json` +## Installation +``` +$ npm install xml2json +``` ## Usage ```javascript var parser = require('xml2json'); var xml = "bar"; -var json = parser.toJson(xml); //returns an string containing the json structure by default +var json = parser.toJson(xml); //returns a string containing the JSON structure by default console.log(json); ``` -* if you want to get the Javascript object then you might want to invoke parser.toJson(xml, {object: true}); -* if you want a reversible json to xml then you should use parser.toJson(xml, {reversible: true}); -* if you want to keep xml space text then you should use `options.space`, `parser.toJson(xml, {space: true})`; +## API + +```javascript +parser.toJson(xml, options); +``` +```javascript +parser.toXml(json, options); +``` + +### Options object + +```javascript +var options = { + object: false, + reversible: false, + coerce: true, + sanitize: true, + trim: true }; +``` + +* **object:** Returns a Javascript object instead of a JSON string +* **reversible:** Makes the JSON reversible to XML (*) +* **coerce:** Makes type coercion. i.e.: numbers and booleans present in attributes and element values are converted from string to its correspondent data types. +* **trim:** Removes leading and trailing whitespaces as well as line terminators in element values. +* **sanitize:** Sanitizes the following characters: + +```javascript +var chars = { '<': '<', + '>': '>', + '(': '(', + ')': ')', + '#': '#', + '&': '&', + '"': '"', + "'": ''' }; +``` + + + + +(*) xml2json tranforms CDATA content to JSON, but it doesn't generate a reversible structure. ## License (The MIT License) -Copyright 2011 BugLabs. All rights reserved. +Copyright 2012 BugLabs. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to