Comments (2)
I agree. Push this and I'll test it, update the version, and kick a new
release sometime next week (currently traveling)
On Aug 16, 2016 8:01 PM, "eliseevmikhail" [email protected] wrote:
xmlToJSON uses single instance for every call, and every call modifies
options object at parseXML function. Is it better to create onetime local
copy of options for every call? Like this:--- xmlToJSON.js-orig 2016-08-17 09:50:51.310936170 +0700
+++ xmlToJSON.js 2016-08-17 09:59:52.543327593 +0700
@@ -57,14 +57,16 @@
};this.parseString = function(xmlString, opt) {
return this.parseXML(this.stringToXML(xmlString), opt);
var localoptions = {}
opt = opt || {}
// initialize options
for (var key in options) {
localoptions[key] = (opt[key] === undefined) ? options[key] : opt[key];
}
return this.parseXML(this.stringToXML(xmlString), localoptions);
}
this.parseXML = function(oXMLParent, opt) {
// initialize options
for (var key in opt) {
options[key] = opt[key];
}
this.parseXML = function(oXMLParent, options) {
var vResult = {},
nLength = 0,
sCollectedTxt = "";
@@ -154,7 +156,7 @@
sProp = oNode.nodeName;
}vContent = xmlToJSON.parseXML(oNode);
vContent = xmlToJSON.parseXML(oNode, options); if (!vResult['_children']) vResult._children = []; vResult._children.push(vContent); vContent._tagname = sProp;
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#25, or mute the thread
https://github.com/notifications/unsubscribe-auth/AFAjfDCdM944P_xjbDmo9m0VrB757n74ks5qgnmGgaJpZM4JmDA7
.
from xmltojson.
I noticed this as well. It looks like subsequent calls to parseString()
preserve the options from the previous call.
> xml = "<ClockCyclesGalore>CPU's working overtime</ClockCyclesGalore>"
'<ClockCyclesGalore>CPU\'s working overtime</ClockCyclesGalore>'
> parsed1 = xmlToJSON.parseString(xml, { textKey: 'CustomTextKey' })
{ ClockCyclesGalore: [ { CustomTextKey: 'CPU\'s working overtime' } ] }
> parsed2 = xmlToJSON.parseString(xml)
{ ClockCyclesGalore: [ { ShouldBeDifferentKey: 'CPU\'s working overtime' } ] }
> parsed1 = xmlToJSON.parseString(xml, { textKey: 'CustomTextKey' })
{ ClockCyclesGalore: [ { CustomTextKey: 'CPU\'s working overtime' } ] }
> parsed2 = xmlToJSON.parseString(xml)
{ ClockCyclesGalore: [ { CustomTextKey: 'CPU\'s working overtime' } ] }
On second call (parsed2
) I would expect the textKey to be the default _text
. However, since options
is modified it is set for subsequent calls.
I recommend:
- Define
options
outside of the core function - In function usage, use
Object.assign
to deep copy theoptions
object.
I'm happy to submit a PR if you'd like.
from xmltojson.
Related Issues (20)
- Add license in bower.json and .min.js HOT 1
- Update module on npm HOT 7
- Cannot iterate children at proper order HOT 7
- When attribute name of an element have ":" character, does not parse HOT 1
- oXMLParent.hasChildNodes is not a function HOT 1
- Add the proper require() statement to your Readme.md file HOT 1
- Cannot get it working with latest angular-cli with webpack HOT 4
- Doesn't work in NodeJS HOT 5
- syntax error display on console when call xml_str2json() on firefox HOT 2
- How to convert XML to JSON in an external reference HOT 3
- How to force to dispaly 0 after decimal? HOT 1
- Implement promises for asynchronous parsing of XML
- Improve examples in README
- Improve test coverage
- Migrate to latest Jasmine (2.8.x)
- Add support for processing instructions and comments HOT 2
- Migrate to DOM4
- Cannot parse Android Manifest XML HOT 4
- Support node v20.13.1 or latest
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xmltojson.