Skip to content
Browse files

Add the end () method

The end() method is a convenience over .doc().toString().
It converts the entire XML document to string.
  • Loading branch information...
1 parent f480bb3 commit 0343ace0e6b6bbf9923c487bfa00f353d4d18e10 @oozcitak committed Aug 31, 2012
Showing with 16 additions and 3 deletions.
  1. +3 −3 README.md
  2. +5 −0 src/XMLBuilder.coffee
  3. +4 −0 src/XMLFragment.coffee
  4. +4 −0 test/test.coffee
View
6 README.md
@@ -20,12 +20,12 @@ As a result, version from v0.1.3 are **not** compatible with previous versions.
``` js
var builder = require('xmlbuilder');
-var doc = builder.create('root')
+var xml = builder.create('root')
.ele('xmlbuilder', {'for': 'node-js'})
.ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
- .doc();
+ .end({ pretty: true});
-console.log(doc.toString({ pretty: true }));
+console.log(xml);
```
will result in:
View
5 src/XMLBuilder.coffee
@@ -97,6 +97,11 @@ class XMLBuilder
return @rootObject
+ # Ends the document and converts string
+ end: (options) ->
+ return toString(options)
+
+
# Converts the XML document to string
#
# `options.pretty` pretty prints the result
View
4 src/XMLFragment.coffee
@@ -233,6 +233,10 @@ class XMLFragment
return @root().documentObject
+ # Ends the document and converts string
+ end: (options) ->
+ return @document().toString(options)
+
# Gets the previous node
prev: () ->
if @isRoot
View
4 test/test.coffee
@@ -214,3 +214,7 @@ xml14 = '<?xml version="1.1"?><test14><node>test</node></test14>'
test14 = xmlbuilder.create('test14', { 'version': '1.1' } ).ele('node').txt('test').doc().toString()
assert.strictEqual(xml14, test14)
+# Test the end() method
+xml15 = '<?xml version="1.1"?><test14><node>test</node></test14>'
+test15 = xmlbuilder.create('test14', { 'version': '1.1' } ).ele('node').txt('test').end()
+assert.strictEqual(xml15, test15)

0 comments on commit 0343ace

Please sign in to comment.
Something went wrong with that request. Please try again.