Trying to parse XML in JSON with xml2js and then return JSON in XML using xmlbuilder (usually after changing the software programmatically).
I think these two should be the complement for this post https://github.com/oozcitak/xmlbuilder-js/issues/69 . But I have some difficulties, and it should be to me that I am not getting the configuration settings correctly.
Here is the code I'm running:
var xml = fs.readFileSync(__dirname + '/../xml/theme.xml', 'utf8');
xml2js.parseString(xml, { attrkey: '@', xmlns: true }, function(err, json) {
var xml2 = xmlbuilder.create(json,
{version: '1.0', encoding: 'UTF-8', standalone: true}
).end({pretty: true, standalone: true})
});
Here is the first bit of the original XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="Office Theme">
<a:themeElements>
<a:clrScheme name="Office">
<a:dk1>
<a:sysClr val="windowText" lastClr="000000"/>
</a:dk1>
<a:lt1>
<a:sysClr val="window" lastClr="FFFFFF"/>
</a:lt1>
<a:dk2>
<a:srgbClr val="1F497D"/>
</a:dk2>
...
</a:themeElements>
</a:theme>
Here is how xml2js parses this into JSON, it looks right:
{
"a:theme": {
"@": {
"xmlns:a": {
"name": "xmlns:a",
"value": "http://schemas.openxmlformats.org/drawingml/2006/main",
"prefix": "xmlns",
"local": "a",
"uri": "http://www.w3.org/2000/xmlns/"
},
"name": {
"name": "name",
"value": "Office Theme",
"prefix": "",
"local": "name",
"uri": ""
}
},
"@ns": {
"uri": "http://schemas.openxmlformats.org/drawingml/2006/main",
"local": "theme"
},
"a:themeElements": [
{
"@ns": {
"uri": "http://schemas.openxmlformats.org/drawingml/2006/main",
"local": "themeElements"
},
"a:clrScheme": [
{
"@": {
"name": {
"name": "name",
"value": "Office",
"prefix": "",
"local": "name",
"uri": ""
}
},
"@ns": {
"uri": "http://schemas.openxmlformats.org/drawingml/2006/main",
"local": "clrScheme"
},
...
Note that in JSON above:
- an attribute (e.g.
name=) turns into keys inside an object @and - attribute values turn into objects
, , xmlbuilder XML:
<a:theme ="[object Object]" ns="[object Object]">
<a:themeElements ns="[object Object]">
<a:clrScheme ="[object Object]" ns="[object Object]">
<a:dk1 ns="[object Object]">
<a:sysClr ="[object Object]" ns="[object Object]"/>
</a:dk1>
<a:lt1 ns="[object Object]">
<a:sysClr ="[object Object]" ns="[object Object]"/>
</a:lt1>
...
</a:themeElements>
</a:theme>
, , XML-: * @ *
: xmlbuilder , :
`{ "@name": "Office Theme"} `
`{ "@" : { "name" : { value: "Office Theme" }}}`
xml2js -, xmlbuilder - , XML → JSON → XML?