Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active April 6, 2020 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenwebb/c33263ca3399e32d902b2903f2e3cff5 to your computer and use it in GitHub Desktop.
Save kenwebb/c33263ca3399e32d902b2903f2e3cff5 to your computer and use it in GitHub Desktop.
Convert between XML and JSON/JS
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Mon Apr 06 2020 06:39:14 GMT-0400 (Eastern Daylight Time)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: Convert between XML and JSON/JS
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: c33263ca3399e32d902b2903f2e3cff5
Keywords:
My Notes
--------
April 3, 2020
http://127.0.0.1:8888/Xholon.html?app=Convert between XML and JSON/JS&src=lstr&gui=clsc&jslib=xml-js.min
http://127.0.0.1:8888/Xholon.html?app=Convert+between+XML+and+JSON%2FJS&src=lstr&gui=clsc&jslib=xml-js.min,js2xmlparser
Xholon .java classes that read JSON, were deactivated when I started using GWT. See:
gwtspace/Xholon/Xholon/src/org/primordion/xholon/io/json
JsonReaderFactory.java.txt
JsonStaxReader.java.txt
JsonStaxReader_jackson.java.txt
JsonWriterFactory.java
JsonStrWriter.java
I should be able to do something like this:
-------------------
var convert = require('xml-js');
//var jsonStr = '{"One": ["Two", "Three", "Four"] }'; // NO
var jsonStr = '{"a":{"b":{}}}';
var options = null; // ???
var xmlStr = convert.json2xml(jsonStr, options); // <a><b/></a>
xh.root().append(xmlStr);
// OR
xh.root().append(convert.json2xml('{"a":{"b":{}}}', options));
this works in Dev Tools
----------
json2xml('{"a":{"b":{}}}', {compact: true, ignoreComment: true, spaces: 4});
result:
"<a>
<b/>
</a>"
and this works
--------------
xh.root().append(json2xml('{"a":{"b":{}}}', {compact: true, ignoreComment: true, spaces: 4}));
xml2json
--------
var xmlStr = '<One><Two/><Three/><Four/></One>';
xml2json(xmlStr, {compact:true});
{"One":{"Two":{},"Three":{},"Four":{}}}
js2xml
------
js2xml(xh.root().first().first(), {compact: true, ignoreComment: true, spaces: 4});
result:
InternalError: too much recursion
js2xmlparser (using Dev Tools)
--------
var obj = {
"firstName": "John",
"lastName": "Smith"
};
var xmlStr = js2xmlparser.parse("person", obj);
console.log(xmlStr);
xh.root().append(xmlStr);
console.log(xh.root().last());
result:
------
<?xml version='1.0'?>
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
</person>
and the IXholon subtree is correctly created
another js2xmlparser example
----------------------------
var obj = {
"firstName": "John",
"lastName": "Smith",
"dateOfBirth": new Date(1964, 7, 26),
"address": {
"@": {
"type": "home"
},
"streetAddress": "3212 22nd St",
"city": "Chicago",
"state": "Illinois",
"zip": 10000
},
"phone": [
{
"@": {
"type": "home"
},
"#": "123-555-4567"
},
{
"@": {
"type": "cell"
},
"#": "890-555-1234"
},
{
"@": {
"type": "work"
},
"#": "567-555-8901"
}
],
"email": "john@smith.com"
};
//console.log(js2xmlparser.parse("person", obj));
var xmlStr = js2xmlparser.parse("person", obj);
console.log(xmlStr);
xh.root().append(xmlStr);
console.log(xh.root().last());
<?xml version='1.0'?>
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<dateOfBirth>Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Daylight Time)</dateOfBirth>
<address type='home'>
<streetAddress>3212 22nd St</streetAddress>
<city>Chicago</city>
<state>Illinois</state>
<zip>10000</zip>
</address>
<phone type='home'>123-555-4567</phone>
<phone type='cell'>890-555-1234</phone>
<phone type='work'>567-555-8901</phone>
<email>john@smith.com</email>
</person>
Note (js2xmlparser):
----
with js2xmlparser, the resulting IXholon subtree does not include the text, which is expected
<person>
<firstName></firstName>
<lastName></lastName>
<dateOfBirth></dateOfBirth>
<address type="home">
<streetAddress></streetAddress>
<city></city>
<state></state>
<zip></zip>
</address>
<phone type="home"></phone>
<phone type="cell"></phone>
<phone type="work"></phone>
<email></email>
</person>
but, if I specify in IH that person and address have childSuperClass="Attribute_String", then I get:
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<dateOfBirth>Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Daylight Time)</dateOfBirth>
<address type="home">
<streetAddress>3212 22nd St</streetAddress>
<city>Chicago</city>
<state>Illinois</state>
<zip>10000</zip>
</address>
<phone>123-555-4567</phone>
<phone>890-555-1234</phone>
<phone>567-555-8901</phone>
<email>john@smith.com</email>
</person>
References
----------
(1) search: json to xml javascript
(2) https://www.npmjs.com/package/xml-js
(3) https://github.com/nashwaan/xml-js
this looks promising
I downloaded the .zip, and unzipped to JavascriptThirdParty/xml-js-master
the .js files in lib look promising
I can probably just use dist/xml-js.min.js
(4) https://github.com/michaelkourlas/node-js2xmlparser
this one also looks interesting
Popular Node.js module for parsing JavaScript objects into XML
this works !
(5) https://github.com/abdolence/x2js
]]></Notes>
<_-.XholonClass>
<!-- domain objects -->
<PhysicalSystem/>
<Block/>
<Brick/>
<!-- quantities -->
<Height superClass="Quantity"/>
<!--<person childSuperClass="Attribute_String"/>
<address childSuperClass="Attribute_String"/>-->
</_-.XholonClass>
<xholonClassDetails>
<Block>
<port name="height" connector="Height"/>
</Block>
</xholonClassDetails>
<PhysicalSystem>
<Block>
<Height>0.1 m</Height>
</Block>
<Brick multiplicity="2"/>
</PhysicalSystem>
<Blockbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var a = 123;
var b = 456;
var c = a * b;
if (console) {
console.log(c);
}
//# sourceURL=Blockbehavior.js
]]></Blockbehavior>
<Heightbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var myHeight, testing;
var beh = {
postConfigure: function() {
testing = Math.floor(Math.random() * 10);
myHeight = this.cnode.parent();
// this works
$wnd.xh.root().append($wnd.json2xml('{"a":{"b":{}}}', {compact: true, ignoreComment: true, spaces: 4}));
/*var obj = {
"firstName": "John",
"lastName": "Smith"
};*/
var obj = {
"firstName": "John",
"lastName": "Smith",
"dateOfBirth": new Date(1964, 7, 26),
"address": {
"@": {
"type": "home"
},
"streetAddress": "3212 22nd St",
"city": "Chicago",
"state": "Illinois",
"zip": 10000
},
"phone": [
{
"@": {
"type": "home"
},
"#": "123-555-4567"
},
{
"@": {
"type": "cell"
},
"#": "890-555-1234"
},
{
"@": {
"type": "work"
},
"#": "567-555-8901"
}
],
"email": "john@smith.com"
};
var xmlStr = $wnd.js2xmlparser.parse("person", obj);
$wnd.console.log(xmlStr);
$wnd.xh.root().append(xmlStr);
$wnd.console.log($wnd.xh.root().last());
},
act: function() {
myHeight.println(this.toString());
},
toString: function() {
return "testing:" + testing;
}
}
//# sourceURL=Heightbehavior.js
]]></Heightbehavior>
<Brickbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
$wnd.xh.Brickbehavior = function Brickbehavior() {}
$wnd.xh.Brickbehavior.prototype.postConfigure = function() {
this.brick = this.cnode.parent();
this.iam = " red brick";
};
$wnd.xh.Brickbehavior.prototype.act = function() {
this.brick.println("I am a" + this.iam);
};
//# sourceURL=Brickbehavior.js
]]></Brickbehavior>
<Brickbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
console.log("I'm another brick behavior");
]]></Brickbehavior>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Block</title>
<rect id="PhysicalSystem/Block" fill="#98FB98" height="50" width="50" x="25" y="0"/>
<g>
<title>Height</title>
<rect id="PhysicalSystem/Block/Height" fill="#6AB06A" height="50" width="10" x="80" y="0"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment