如何在nodejs中通过xpath插入节点,typescript?



我正试图使一些代码通过xpath插入节点到通用配置。
通常我在写。net应用程序,但现在我需要在nodejs上写工具,类型script
我不知道怎么写对了如何使**childNodes的cast[0]**所以我可以调用**appendChild**?
import {DOMParser as dom} from "xmldom";
import * as xpath from "xpath";
export default class FileJob extends BaseJob {
    async execute(settings: JobSettings) {
            this.poke('C:\Temp\LocalCache\Config.xml','/Root','<DB-Connection id="development" database="development"/>');
    }
    private poke(path:fs.PathLike,pathToNode:string, node : string)
    {
        const file = fs.readFileSync(path,'utf-8');
        var doc = new dom().parseFromString(file);
        var tempDoc = new dom().parseFromString(node);
        var importedNode = doc.importNode(tempDoc.documentElement, true);
        var childNodes = xpath.select(pathToNode, doc);
        if (childNodes[0]!==undefined)
        {
            //What to do here?
            //Property 'appendChild' does not exist on type 'SelectedValue'.
            //Property 'appendChild' does not exist on type 'string'.
            childNodes[0].appendChild(importedNode);
            fs.writeFileSync(path,doc.toString());
        }
    }
}

我又笨又年轻。

import {DOMParser as dom} from "xmldom";
import * as xpath from "xpath";
export default class FileJob extends BaseJob {
    async execute(settings: JobSettings) {
            this.poke('C:\Temp\LocalCache\Config.xml','/Root','<DB-Connection id="development" database="development"/>');
    }
    private poke(path:fs.PathLike,pathToNode:string, node : string)
    {
        const file = fs.readFileSync(path,'utf-8');
        var doc = new dom().parseFromString(file);
        var tempDoc = new dom().parseFromString(node);
        var importedNode = doc.importNode(tempDoc.documentElement, true);
        var childNodes = xpath.select(pathToNode, doc);
        if (childNodes[0]!==undefined)
        {
          let currentNode = childNodes[0] as Node;
          currentNode.appendChild(importedNode);
          fs.writeFileSync(path,doc.toString());
        }
    }
}

最新更新