Merge pull request #7 from pludov/preserve_text
Add option verbatimText option to preserve blanks in text
This commit is contained in:
28
README.md
28
README.md
@@ -228,6 +228,34 @@ npm install xml-streamer
|
||||
```
|
||||
`caution:` When explicitArray set to false and if there are multiple children nodes with same name then last node will override all preceding nodes.
|
||||
|
||||
* `verbatimText`: `Type: Boolean` Optional field. `Default value is false`. When set, text attribute will include all blanks found in xml. When unset, blanks are removed as long as they come in one expat single block (blank lines, newlines and entities).
|
||||
|
||||
// Ex: For example let the XML be
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<items>
|
||||
<item>
|
||||
This is
|
||||
a test
|
||||
</item>
|
||||
</items>
|
||||
```
|
||||
// if verbatimText is true and resourcePath is /items/item.
|
||||
// Output for above xml will be
|
||||
```javascript
|
||||
[
|
||||
{ '_' : "\nThis is\na test\n "}
|
||||
]
|
||||
```
|
||||
|
||||
// if verbatimText is false and resourcePath is /items/item.
|
||||
// Output for above xml will be
|
||||
```javascript
|
||||
[
|
||||
{ '_' : "This isa test"}
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
## upcoming features
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@ var defaults = {
|
||||
emitOnNodeName: false,
|
||||
attrsKey: '$',
|
||||
textKey: '_',
|
||||
explicitArray: true
|
||||
explicitArray: true,
|
||||
verbatimText: false
|
||||
}
|
||||
|
||||
function XmlParser (opts) {
|
||||
@@ -89,6 +90,7 @@ function registerEvents () {
|
||||
var textKey = this.opts.textKey
|
||||
var interestedNodes = state.interestedNodes
|
||||
var explicitArray = this.opts.explicitArray
|
||||
var verbatimText = this.opts.verbatimText;
|
||||
|
||||
parser.on('startElement', function (name, attrs) {
|
||||
if (state.isRootNode) validateResourcePath(name)
|
||||
@@ -185,7 +187,7 @@ function registerEvents () {
|
||||
}
|
||||
|
||||
function processText (text) {
|
||||
if (!text || !/\S/.test(text)) {
|
||||
if ((!text) || ((!verbatimText) && !/\S/.test(text))) {
|
||||
return
|
||||
}
|
||||
var path = getRelativePath()
|
||||
|
||||
Reference in New Issue
Block a user