以节点 js

问题描述

我对 NodeJS 很陌生,如果我的查询太基本,请读者们多多包涵。

我有一个 xml 文件,它很容易使用 DOMParser 和 querySelector 来解析和定位某些节点,并且前端(浏览器)的 javascript ......真的是后端的一部分..这就是我正在尝试的..

fetch(`http://localhost:3000/data/rogan/gallery.xml`)
    .then((response) => response.text())
    .then((xml) => {
      const xmlDOM = new JSDOM(xml,{
        contentType: "text/xml",features: {
          QuerySelector: true,},});

      const images = xmlDOM.querySelector("img");
      console.log(images);
      res.send(images);
    })
    .catch((err) => console.log(err));
});

在node中使用querySelector有错吗?我的意思是这是前端的东西而不是我们在节点中使用的东西?

我能够获取 XML,但我不确定如何将 xml 中的某些特定节点定位到我想要推送到数组或对象的特定节点(我还没有达到那个阶段,但首先我需要定位节点)..

任何帮助/建议将不胜感激。

解决方法

您似乎以错误的方式调用了 querySelector。这是我在 JSdom 的文档中找到的内容:

dom.window.document.querySelector("p").textContent

就你而言,我应该是

xmlDOM.window.document.querySelector("img");