I'm trying to check if a pair (element, value) exists in XML. I'm using xmldoc
XML file:
<?xml version="1.0"?>
<listings>
<listing>
<id>1</id>
<name>TEST1</name>
<description>TEST1</description>
</listing>
<listing>
<id>2</id>
<name>TEST</name>
<description>TEST</description>
</listing>
</listings>
My code:
const fs = require('fs');
const path = require("path");
const xmldoc = require('xmldoc');
const xml = fs.readFileSync(path.resolve(__dirname, "./myXML"), 'utf8');
const doc = new xmldoc.XmlDocument(xml);
const check = doc.childWithAttribute('id', '2');
console.log(check);
It's returning undefined, but I have an element with the id = 2.
Does anyone know what I am doing wrong? Also, after finding the element, is it possible to delete it?
Thanks
childWithAttributefunctionSearches for the first child with the given attribute value.. I assume this means it will not recursively search through your doc. You will have to create the code to check the deeper levelschildNamed