Roo-Code/src/utils/xml.ts
Matt Rubens d0661fb0c7
Don't automatically convert types when parsing XML (#2389)
* Don't automatically convert types when parsing XML

* PR feedback
2025-04-07 14:57:02 -04:00

27 lines
828 B
TypeScript

import { XMLParser } from "fast-xml-parser"
/**
* Parses an XML string into a JavaScript object
* @param xmlString The XML string to parse
* @returns Parsed JavaScript object representation of the XML
* @throws Error if the XML is invalid or parsing fails
*/
export function parseXml(xmlString: string, stopNodes?: string[]): unknown {
const _stopNodes = stopNodes ?? []
try {
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "@_",
parseAttributeValue: false,
parseTagValue: false,
trimValues: true,
stopNodes: _stopNodes,
})
return parser.parse(xmlString)
} catch (error) {
// Enhance error message for better debugging
const errorMessage = error instanceof Error ? error.message : "Unknown error"
throw new Error(`Failed to parse XML: ${errorMessage}`)
}
}