node-xml2js icon indicating copy to clipboard operation
node-xml2js copied to clipboard

Missing feature - handling XML processing instructions

Open pawel-mika opened this issue 9 years ago • 2 comments

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.

pawel-mika avatar Oct 20 '16 12:10 pawel-mika

+1

usualdesigner avatar Mar 16 '21 13:03 usualdesigner

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

flakey-bit avatar Jul 15 '21 03:07 flakey-bit