title: Parse XML to JSON and back ... with xml2js and xmlbuilder?
body: <p>Trying to parse XML into JSON with <a href="https://github.com/Leonidas-from-XIV/node-xml2js" rel="noreferrer">xml2js</a> and then return the JSON to XML using <a href="https://github.com/oozcitak/xmlbuilder-js/" rel="noreferrer">xmlbuilder</a> (usually after modifying the content programmatically).</p>
  
  <p>I <em>think</em> that the two should be complements, per this post <a href="https://github.com/oozcitak/xmlbuilder-js/issues/69" rel="noreferrer">https://github.com/oozcitak/xmlbuilder-js/issues/69</a>.  But am having some difficulty, and it's gotta be that I'm not getting the config parameters right.</p>
  
  <p>Here's the code I'm running:</p>
  
  <pre><code>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})
  });
  </code></pre>
  
  <p>Here's the first bit of the original XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="Office Theme"&gt;
      &lt;a:themeElements&gt;
          &lt;a:clrScheme name="Office"&gt;
              &lt;a:dk1&gt;
                  &lt;a:sysClr val="windowText" lastClr="000000"/&gt;
              &lt;/a:dk1&gt;
              &lt;a:lt1&gt;
                  &lt;a:sysClr val="window" lastClr="FFFFFF"/&gt;
              &lt;/a:lt1&gt;
              &lt;a:dk2&gt;
                  &lt;a:srgbClr val="1F497D"/&gt;
              &lt;/a:dk2&gt;
              ...
       &lt;/a:themeElements&gt;           
   &lt;/a:theme&gt;
  </code></pre>
  
  <p>Here;s how xml2js parses that to JSON, this looks right to me:</p>
  
  <pre><code>{
      "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"
                          },
                         ...
  </code></pre>
  
  <p>Note that in the JSON above:</p>
  
  <ol>
  <li>the attribute (e.g. <code>name=</code>) are turned into keys inside an <code>@</code> object and </li>
  <li>the attribute values are turned into objects </li>
  </ol>
  
  <p>Now here's how it looks when xmlbuilder turns it back into XML:</p>
  
  
  
  <pre><code>&lt;a:theme ="[object Object]" ns="[object Object]"&gt;
    &lt;a:themeElements ns="[object Object]"&gt;
      &lt;a:clrScheme ="[object Object]" ns="[object Object]"&gt;
        &lt;a:dk1 ns="[object Object]"&gt;
          &lt;a:sysClr ="[object Object]" ns="[object Object]"/&gt;
        &lt;/a:dk1&gt;
        &lt;a:lt1 ns="[object Object]"&gt;
          &lt;a:sysClr ="[object Object]" ns="[object Object]"/&gt;
        &lt;/a:lt1&gt;
         ...
      &lt;/a:themeElements&gt;
   &lt;/a:theme&gt;
  </code></pre>
  
  <p>So there are two problems that XML builder is facing:
    * it's not recognizing the attribute names within the <code>@</code> object and
    * it's not recognizing the attribute value within the attribute object</p>
  
  <p>Hacking it appears that xmlbuilder wants attributes names structured like: </p>
  
  <pre><code>  `{ "@name": "Office Theme"} `
  </code></pre>
  
  <p>rather than </p>
  
  <pre><code>  `{ "@" : { "name" : { value: "Office Theme" }}}`
  </code></pre>
  
  <p>Should I configure xml2js differently, xmlbuilder differently, or is there a different pair of libraries that can parse XML -> JSON -> XML?</p>