安装fast-xml-parser@3.3.4
npm install fast-xml-parser@3.3.4
attributeNamePrefix: '@_',
attrNodeName: '@', //default is false
ignoreAttributes: false,
attrValueProcessor: (val, attrName) =>
he.decode(val, { isAttributeValue: true }), //default is a=>a
tagValueProcessor: (val, tagName) => he.decode(val), //default is a=>a
export const mbbalqry = {
stream: [
{
action: 'MBBALQRY',
userName: 'citic',
list: [
{
row: [
{
accountNo: '8110801013201236512',
},
],
'@_name': 'userDataList',
},
],
bankType: 'CITIC',
startRecord: '1',
pageNumber: '20',
},
],
};
import * as fxp from 'fast-xml-parser';
import * as he from 'he';
export const objToXml = (obj: Object) => {
var Parser = fxp.j2xParser;
//default options need not to set
var defaultOptions = {
attributeNamePrefix: '@_',
attrNodeName: '@', //default is false
textNodeName: '#text',
ignoreAttributes: false,
ignoreNameSpace: false,
cdataTagName: false, //default is false
cdataPositionChar: '\\c',
format: true,
indentBy: ' ',
supressEmptyNode: false,
attrValueProcessor: (val, attrName) =>
he.decode(val, { isAttributeValue: true }), //default is a=>a
tagValueProcessor: (val, tagName) => he.decode(val), //default is a=>a
};
const parser = new Parser(defaultOptions);
let xmlstr = parser.parse(obj);
xmlstr = '<?xml version="1.0" encoding="GBK"?>' + xmlstr;
return xmlstr;
};
<?xml version="1.0" encoding="GBK"?><stream>
<action>MBBALQRY</action>
<userName>citic</userName>
<list name="userDataList">
<row>
<accountNo>8110801013201236512</accountNo>
</row>
</list>
<bankType>CITIC</bankType>
<startRecord>1</startRecord>
<pageNumber>20</pageNumber>
</stream>