cyrfer / x2js

Automatically exported from code.google.com/p/x2js
0 stars 2 forks source link

'arrays in arrays' #13

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Looking for a way to avoid the "old" <element>_asArray and still have the 
opportunity to have nested multiple elements (arrays in arrays)

Example:
-------------
var x2js = new X2JS();
var json = x2js.xml2json(xmldoc);
x2js.asArray(json.setup.icons.icon);
x2js.asArray(json.setup.templates.template); //asArray OK
x2js.asArray(json.setup.templates.template.states.state); //DOOOOhhh!!
-------------
Uncaught TypeError: Cannot read property 'state' of undefined 
-------------

It makes sense. Fundamental stufff :) I need to loop all states and call 
asArray on each template.

But it would be real nice if there was a way anyway.. Maybe a naming 
convention... something like:

var x2js = new X2JS();
x2js.becomesArray("json.setup.icons.icon");
x2js.becomesArray("json.setup.templates.template"); 
x2js.becomesArray("json.setup.templates.template.states.state"); 
var json = x2js.xml2json(xmldoc); 

Original issue reported on code.google.com by jmj.bak...@gmail.com on 11 Oct 2013 at 9:11

GoogleCodeExporter commented 9 years ago
Hmm... It is interesting idea. Thanks! I'll think about it.

Original comment by abdulla....@gmail.com on 11 Oct 2013 at 9:32

GoogleCodeExporter commented 9 years ago
And Thank you! I forgot :) 
Love this lib. 
Will just stroll down the "old" _asArray-road for now. NP!

Inspiration could by the way be found here (in actionscript):
http://krasimirtsonev.com/blog/article/as3-action-script-convert-xml-to-json-xml
2json 

Original comment by jmj.bak...@gmail.com on 11 Oct 2013 at 9:48

GoogleCodeExporter commented 9 years ago
I've just added a new feature to v1.1.3
(You always can look it at 
https://code.google.com/p/x2js/source/browse/xml2json.js or download from 
https://x2js.googlecode.com/hg/xml2json.js)

You can do now like this, for your example:
var x2js = new X2JS({
   arrayAccessFormPaths : ["json.setup.templates.template", "json.setup.templates.template.states.state"]
});
var json = x2js.xml2json(xmldoc);

And access like the other JS objs:
json.setup.templates.template[0].states.state

You can specify also paths to arrays as RegEx form:
var x2js = new X2JS({
   arrayAccessFormPaths : [/.*\.template/, /.*\.state/]
});

Let me know if it works for you! Thanks!

Original comment by abdulla....@gmail.com on 12 Oct 2013 at 11:11

GoogleCodeExporter commented 9 years ago
Also if you need some powerful XPath like tool on JSon there some additional 
libraries. 
For example I've just googled it - http://goessner.net/articles/JsonPath/ :)

Original comment by abdulla....@gmail.com on 12 Oct 2013 at 11:36

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Perfect!  Nothing less.
Rewrote my project to use the arrayAccessFormPath  and it simply worked. 
Thank you!

Haven't tested the json xpath yet. But I sure will. 

Original comment by jmj.bak...@gmail.com on 14 Oct 2013 at 8:32

GoogleCodeExporter commented 9 years ago
I appreciate for your feedback and interest! Thank you!
Well, I'm closing this one for now then.

Original comment by abdulla....@gmail.com on 14 Oct 2013 at 8:38