diff --git a/package.json b/package.json index 623fa77..6707c25 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,8 @@ "license": "ISC", "dependencies": { "get-doctype": "^1.0.0", + "lodash": "^3.10.1", + "node-expat": "^2.3.12", "xml-mapping": "^1.7.0", "xmldom": "^0.1.19", "xpath": "0.0.9" diff --git a/tools/sax-parser-sample.js b/tools/sax-parser-sample.js new file mode 100755 index 0000000..efdcc2c --- /dev/null +++ b/tools/sax-parser-sample.js @@ -0,0 +1,59 @@ +#!/usr/bin/env node + +var expat = require('node-expat'); +var parser = new expat.Parser("UTF-8"); +var fs = require("fs"); +var _ = require("lodash"); + +process.stdin.setEncoding('utf8'); + +var profondeur = 0; + +// Évément déclenché sur balise ouvrante +parser.on('startElement', function(name, attrs) { + + //affichage du nom de l'élément ouvrant + console.log(indentation() + "{ nomElement : \"" + name + "\""); + + //affichage des attributs si pertinent + if (Object.keys(attrs).length > 0) { + _.forIn(attrs, function(attrValue, attrName) { + console.log(indentation() + " attributs : ["); + console.log(indentation() + " { nom: \"%s\", valeur: \"%s\"}", attrName, attrValue); + console.log(indentation() + " ]"); + }); + } + + profondeur++; + +}); + +// Évément déclenché sur balise fermante +parser.on('endElement', function(name) { + profondeur--; + console.log(indentation() + "}"); +}); + +// Évément déclenché sur noeud texte +parser.on('text', function(txt) { + //affichage des noeuds texte non vide + if (txt.trim() !== "") { + console.log(indentation() + "text : \"" + txt + "\"") ; + } +}); + +// Évément déclenché sur commentaire +parser.on('comment', function(txt) { + //affichage des commentaires + console.log(indentation() + "comment : \"" + txt + "\""); +}); + +var indentation = function() { + var res = ""; + for (var i=0; i < profondeur; i++) res += " "; + return res; +}; + +var xmlContent = fs.readFileSync(process.argv[2]); + +parser.write(xmlContent); diff --git a/xml/minimal.xml b/xml/minimal.xml new file mode 100644 index 0000000..20fc9f9 --- /dev/null +++ b/xml/minimal.xml @@ -0,0 +1,16 @@ + + + Outils XML ISTEX + + + + + Niederlender + Claude + + + Caron + Étienne + + +