node xml2js

2020-12-16  本文已影响0人  antlove
const { Builder, parseString } = require('xml2js');

/** 生成xml */
const builder = new Builder({headless: true});
const obj = {name: 'super', bags: {bag: ['A', 'B']}}
const xml = builder.buildObject(obj);

/*
    <root>
        <name>super</name>
        <bags>
            <bag>A</bag>
            <bag>B</bag>
        </bags>
    </root>
*/

/** 解析xml */
parseString(xml, (err, content) => console.log(JSON.stringify(content, null, 4)));
/*
    {
        "root": {
            "name": [
                "super"
            ],
            "bags": [
                {
                    "bag": [
                        "A",
                        "B"
                    ]
                }
            ]
        }
    }
*/
上一篇 下一篇

猜你喜欢

热点阅读