const xml = `
<user>
<name>ajanuw</name>
<age>14</age>
</user>
<user>
<name>alone</name>
<age>12</age>
</user>
`;
const fxp = require("fast-xml-parser");
const xml2json = fxp.parse(xml);
console.log(JSON.stringify(xml2json)); // {"user":[{"name":"ajanuw","age":14},{"name":"alone","age":12}]}
const obj = {
users: [
{name: 'ajanuw'},
{name: 'alone'}
]
}
const obj2xml = new fxp.j2xParser().parse(obj)
console.log( obj2xml ) // <users><name>ajanuw</name></users><users><name>alone</name></users>
增加配置
const fxp = require("fast-xml-parser");
const obj = {
xml: {
_attrs: {
version: "1.0.1"
},
user: [
{
_attrs: {
key: 1
},
name: {
_cdata: "ajanuw",
_attrs: {
age: 14,
height: 166
}
}
},
{
_attrs: {
key: 2
},
name: {
"#text": "alone",
_attrs: {
age: 12
}
}
}
]
}
};
const obj2xml = new fxp.j2xParser({
format: true,
attrNodeName: "_attrs",
textNodeName: "#text",
cdataTagName: "_cdata"
}).parse(obj);
console.log(obj2xml);
<xml version="1.0.1">
<user key="1">
<name age="14" height="166">
<![CDATA[ajanuw]]>
</name>
</user>
<user key="2">
<name age="12">alone</name>
</user>
</xml>
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。