21 lines
443 B
TypeScript
21 lines
443 B
TypeScript
|
import type { ChildNode } from 'domhandler'
|
||
|
import { DomHandler } from 'domhandler'
|
||
|
import { Parser } from 'htmlparser2'
|
||
|
|
||
|
export function xmlToDom(xml: string) {
|
||
|
let _error: Error | null = null
|
||
|
let _dom: ChildNode[] | null = null
|
||
|
|
||
|
const handler = new DomHandler((error, dom) => {
|
||
|
_error = error
|
||
|
_dom = dom
|
||
|
})
|
||
|
const parser = new Parser(handler)
|
||
|
parser.write(xml)
|
||
|
parser.end()
|
||
|
|
||
|
if (_error) throw _error
|
||
|
|
||
|
return _dom!
|
||
|
}
|