S
S
Svyatoslav Khusamov2019-10-23 11:56:23
Node.js
Svyatoslav Khusamov, 2019-10-23 11:56:23

Is there a replacement for the deprecated jindw/xmldom?

Hey!
The https://github.com/jindw/xmldom package has been deprecated specifically.
Haven't found anything more or less new yet.
Maybe someone is already using a more advanced library for the XML DOM?
Ideally, I would like:
1) the package to work equally on the server and in the browser
2) so that all classes are exported in it, including Element for example
3) typescript support

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Svyatoslav Khusamov, 2019-10-23
@khusamov

So far I found this option from jsdom:

import {JSDOM} from 'jsdom';

declare module 'jsdom' {
  /**
   * В пакете jsdom есть реализация XMLSerializer, но пока не описана в типах.
   */
  export interface DOMWindow {
    XMLSerializer: {
      prototype: XMLSerializer;
      new(): XMLSerializer;
    };
  }
}

/**
 * Такой вариант извлечения нужных классов для работы с XML DOM подсказан на странице
 * https://stackoverflow.com/questions/11398419/trying-to-use-the-domparser-with-node-js
 */
const window = new JSDOM().window;

export const {Element, XPathResult, XMLSerializer} = window;
export const createDocument = window.document.implementation.createDocument.bind(window.document.implementation);

/**
 * Извлечение массива с найденными узлами из XPathResult, который возвращает метод ownerDocument.evaluate().
 * При условии, что последний аргумент evaluate был выставлен как XPathResult.*_SNAPSHOT_TYPE.
 * @link https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/snapshotItem
 */
export function getNodesFromXPathResult(result: XPathResult): Node[] {
  let nodes: Node[] = [], node: Node | null, i = 0;
  while (!!(node = result.snapshotItem(i++))) nodes.push(node);
  return nodes;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question