You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.4 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import {XMLParser} from 'fast-xml-parser';
/**
* Parses XML data into a JavaScript object.
* @param {string} xmlData - The XML data to parse.
* @returns {Object|null} - The parsed object or null if parsing fails.
*/
export const parseXML = (xmlData) =>{
const parser = new XMLParser({
attributeNamePrefix: '', // 为属性添加前缀
attrNodeName: '', // 属性节点名称
textNodeName: '#text', // 文本节点名称
ignoreAttributes: false, // 是否忽略属性
format: true, // 是否格式化输出
indentBy: ' ', // 缩进字符
});
try{
const result = parser.parse(xmlData);
return result;
}catch (error) {
console.error("Error parsing XML:", error);
return null;
}
}
/**
* 解析XML数据无属性
* @param {*} xmlData XML数据
* @returns
*/
export const parseXMLWithNoAttr=(xmlData)=>{
const parser = new XMLParser({
attributeNamePrefix: '@_', // 为属性添加前缀
attrNodeName: '@', // 属性节点名称
textNodeName: '#text', // 文本节点名称
ignoreAttributes: false, // 是否忽略属性
format: true, // 是否格式化输出
indentBy: ' ', // 缩进字符
});
try{
const result = parser.parse(xmlData);
return result;
}catch (error) {
console.error("Error parsing XML:", error);
return null;
}
}