Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.swp
*.DS_Store
56 changes: 48 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<foo>bar</foo>";
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 = { '<': '&lt;',
'>': '&gt;',
'(': '&#40;',
')': '&#41;',
'#': '&#35;',
'&': '&amp;',
'"': '&quot;',
"'": '&apos;' };
```




(*) 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
Expand Down