0

我正在使用fast-xml-parser从 json 创建 xml 文档。在示例中,他们展示了如何创建具有属性的元素

const jsOrderedObj = [
    {
        "?textinfo": [
            {
                "#text": ""
            }
        ],
        ":@": {
            "@_whitespace": true,
            "@_is": true,
            "@_allowed": true
        }
    }
];
const options = {
    ignoreAttributes: false,
    preserveOrder: true,
    allowBooleanAttributes: true,
    suppressBooleanAttributes: true
};
const builder = new XMLBuilder(options);
const output = builder.build(result);

结果:

<?textinfo   whitespace is allowed?>

以及如何使用子元素创建 XML 文档的示例:

const xmlData = {
            'foo': {
                'bar': 'data',
                'bar2': 'data2',
                'bar3': 'data3',
                'bar4': 'data4',
            }
        }
const builder = new XMLBuilder({});
const output = builder.build(xmlData);

结果:

<foo><bar>data</bar><bar2>data2</bar2><bar3>data3</bar3><bar4>data4</bar4></foo>

现在我的问题是,我应该如何定义我的 json,以在我的 xml 中获取属性和子元素,如下所示:

<foo whitespace is allowed><bar>data</bar><bar2>data2</bar2><bar3>data3</bar3><bar4>data4</bar4></foo>
4

0 回答 0