Convert XML to JSON (and back) using Javascript

后端 未结 12 1507
Happy的楠姐
Happy的楠姐 2020-11-22 03:18

How would you convert from XML to JSON and then back to XML?

The following tools work quite well, but aren\'t completely consistent:

  • xml2json
12条回答
  •  无人共我
    2020-11-22 03:24

    You can also use txml. It can parse into a DOM made of simple objects and stringify. In the result, the content will be trimmed. So formating of the original with whitespaces will be lost. But this could be used very good to minify HTML.

    const xml = require('txml');
    const data = `
    tag content
    another content
    
      inside content
      
    `;
    
    const dom = xml(data); // the dom can be JSON.stringified
    
    xml.stringify(dom); // this will return the dom into an xml-string
    

    Disclaimer: I am the author of txml, the fastest xml parser in javascript.

提交回复
热议问题