I'm trying to make some code to insert node into generic config by xpath.
Usually I'm writing .Net applications but now I need to write tool on nodejs, type script
I don't get it how write it right How to make cast of **childNodes[0]** so I can call **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());
}
}
}
CodePudding user response:
I was stupid and young.
Here is what was necessary.
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());
}
}
}