40 lines
1014 B
TypeScript
40 lines
1014 B
TypeScript
import * as plugins from '../plugins.js';
|
|
|
|
// Create a singleton instance of SmartXml
|
|
const smartXmlInstance = new plugins.SmartXml();
|
|
|
|
/**
|
|
* Parse XML string to JavaScript object
|
|
*/
|
|
export function parseXml(xmlString: string): any {
|
|
return smartXmlInstance.parseXmlToObject(xmlString);
|
|
}
|
|
|
|
/**
|
|
* Convert JavaScript object to XML string with XML declaration
|
|
*/
|
|
export function createXml(obj: any, options: { format?: boolean } = {}): string {
|
|
const xml = smartXmlInstance.createXmlFromObject(obj);
|
|
|
|
// Ensure XML declaration is present
|
|
if (!xml.startsWith('<?xml')) {
|
|
return `<?xml version="1.0" encoding="UTF-8"?>\n${xml}`;
|
|
}
|
|
|
|
return xml;
|
|
}
|
|
|
|
/**
|
|
* Helper to create S3-compatible XML responses with proper namespace
|
|
*/
|
|
export function createS3Xml(rootElement: string, content: any, namespace = 'http://s3.amazonaws.com/doc/2006-03-01/'): string {
|
|
const obj: any = {
|
|
[rootElement]: {
|
|
'@_xmlns': namespace,
|
|
...content,
|
|
},
|
|
};
|
|
|
|
return createXml(obj, { format: true });
|
|
}
|