node-xml2js
node-xml2js copied to clipboard
Missing feature - handling XML processing instructions
Hi I wonder if there's a possibility to add this little feature available in xmlbuilder - https://github.com/oozcitak/xmlbuilder-js/wiki#processing-instructions? Currently I need to do rather hacky-way, by string comparision and then insert/replace.
+1
If we just wanted to support collecting processing instructions we could allow the calling code to do this pretty easily - something like this:
diff --git a/src/parser.coffee b/src/parser.coffee
index eb19c5b..03d8f47 100644
--- a/src/parser.coffee
+++ b/src/parser.coffee
@@ -87,6 +87,9 @@ class exports.Parser extends events
@saxParser.ended = true
@emit "end", @resultObject
+ @saxParser.onprocessinginstruction = (instruction) =>
+ @emit "processinginstruction", instruction
+
# another hack to avoid throwing exceptions when the parsing has ended
# but the user-supplied callback throws an error
@saxParser.ended = false
i.e. just add a callback to the underlying sax parser for onprocessinginstruction and just emit the event verbatim.
and then in the calling code
const parser = new xmljs.Parser();
parser.addListener('processinginstruction', (instr) => {
// do something with instr
});
await parser.parseStringPromise(xml);