GOOGLE ADS

sábado, 16 de abril de 2022

¿Cómo insertar un nodo por xpath en nodejs, mecanografiado?

Estoy tratando de hacer un código para insertar el nodo en la configuración genérica por xpath.
Por lo general, escribo aplicaciones.Net, pero ahora necesito escribir una herramienta en nodejs, escribir script
. No entiendo cómo escribirlo correctamente. Cómo hacer una conversión de **childNodes[0]** para poder llamar a **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());
}
}
}


Solución del problema

Yo era estúpido y joven.
Aquí está lo que era necesario.

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());
}
}
}

No hay comentarios.:

Publicar un comentario

Flutter: error de rango al acceder a la respuesta JSON

Estoy accediendo a una respuesta JSON con la siguiente estructura. { "fullName": "FirstName LastName", "listings...