Skip to content

Add parseStringSync #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ not, we got you covered! Starting with 0.2.8 you can also leave it out, in
which case `xml2js` will helpfully add it for you, no bad surprises and
inexplicable bugs!

Synchronous usage
------------------

Forced into a sync use-case? Use the `parseStringSync` method to execute parsing
in a synchronous manner. Failures will result in an thrown error.

```javascript
var fs = require('fs'),
xml2js = require('xml2js');

var data = fs.readFileSync(__dirname + '/foo.xml', 'utf8');

// With parser
var parser = new xml2js.Parser(/* options */);
var result = parser.parseStringSync(data);

// Without parser
var result = xml2js.parseStringSync(data /*, options */);
```

Parsing multiple files
----------------------

Expand Down
25 changes: 25 additions & 0 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/xml2js.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@
"lib": "./lib"
},
"scripts": {
"build": "cake build",
"test": "zap",
"coverage": "nyc npm test && nyc report",
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
"doc": "cake doc"
},
"repository": {
"type": "git",
Expand Down
22 changes: 21 additions & 1 deletion src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ class exports.Parser extends events.EventEmitter
else if @saxParser.ended
throw err

parseStringSync: (str) =>
# SAX events block when given a string instead of a stream
result = undefined
err = undefined
cb = (_err, _result) ->
result = _result
err = _err

# parseString will implicitly call cb (thus setting closure result) before returning
@parseString str, cb
if err
throw err
result

exports.parseString = (str, a, b) ->
# let's determine what we got as arguments
if b?
Expand All @@ -267,6 +281,12 @@ exports.parseString = (str, a, b) ->
# and options should be empty - default
options = {}

# the rest is super-easy
parser = new exports.Parser options
parser.parseString str, cb

exports.parseStringSync = (str, a) ->
if typeof a == 'object'
options = a

parser = new exports.Parser options
parser.parseStringSync str
1 change: 1 addition & 0 deletions src/xml2js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ exports.Builder = builder.Builder
exports.Parser = parser.Parser

exports.parseString = parser.parseString
exports.parseStringSync = parser.parseStringSync
45 changes: 45 additions & 0 deletions test/parser.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,48 @@ module.exports =
console.log 'Result object: ' + util.inspect r, false, 10
equ r.hasOwnProperty('SAMP'), true
equ r.SAMP.hasOwnProperty('TAGN'), true)

'test sync parsing': (test) ->
x2js = new xml2js.Parser()
data = fs.readFileSync fileName, 'utf8'
r = x2js.parseStringSync data
assert.notEqual r, null
equ r.sample.listtest[0].item[0].subitem[0], 'Foo(1)'
test.finish()

'test sync with bad input': (test) ->
x2js = new xml2js.Parser()
data = fs.readFileSync fileName, 'utf8'
err = null
try
r = x2js.parseStringSync "< a moose bit my sister>";
catch _err
err = _err
assert.notEqual err, null
test.finish()

'test global sync parsing': (test) ->
data = fs.readFileSync fileName, 'utf8'
r = xml2js.parseStringSync data
assert.notEqual r, null
equ r.sample.listtest[0].item[0].subitem[0], 'Foo(1)'
test.finish()

'test global sync parsing with options': (test) ->
data = fs.readFileSync fileName, 'utf8'
r = xml2js.parseStringSync data,
trim: true
normalize: true
console.log r
equ r.sample.whitespacetest[0]._, 'Line One Line Two'
test.finish()

'test global sync with bad input': (test) ->
data = fs.readFileSync fileName, 'utf8'
err = null
try
r = xml2js.parseStringSync "< a moose bit my sister>";
catch _err
err = _err
assert.notEqual err, null
test.finish()