I am using xml2js
to convert an xml
file into a javascript object. I need to identify attributes within element tags in the xml
file and manipulate the matching element(s).
For example, I have some a property
element in test.xml
:
...
<property name="TemporaryPort" type="input" required="true" order="0">
<object>endpoint</object>
</property>
<property name="TemporaryPort" type="input" required="false" order="1">
<object>endpoint</object>
</property>
<property name="TemporaryPort" type="output" required="false" order="2">
<object>endpoint</object>
</property>
...
In my parse.js
file that is parsing this xml
file, I would like to identify elements based on their attributes:
const xml2js = require('xml2js');
const xml = fs.readFileSync("test.xml");
let output = "";
const parser = new xml2js.Parser();
parser.parseString(xml, function(err,result){
// I would like to find all elements where:
// required is set to "true",
// name is set to "TemporaryPort", and
// type is set to "input"
output="";
console.log(output);
});
Maybe with DOMParser() ?
sample code: