Skip to content

Instantly share code, notes, and snippets.

@scripting
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scripting/bcd76324a27b5567874b to your computer and use it in GitHub Desktop.
Save scripting/bcd76324a27b5567874b to your computer and use it in GitHub Desktop.
Question about serializing XML structures in JavaScript. See comment below for explanation.
function xmlVisit (adrx, callback, level, path) {
if (level === undefined) {
level = 0;
}
if (path === undefined) {
path = "";
}
$(adrx).children ("outline").each (function () {
var flvisitsubs = true, name = xmlGetNodeName (this);
xmlExpandInclude (this);
if (callback != undefined) {
if (!callback (this, level, path + name)) {
flvisitsubs = false;
}
}
if (flvisitsubs) {
if (!xmlVisit (this, callback, level + 1, path + name + "/")) {
return (false);
}
}
});
return (true);
}
function opmlEmojify (opmltext) {
var xstruct = $($.parseXML (opmltext));
var adropml = xmlGetAddress (xstruct, "opml");
var adrhead = xmlGetAddress (adropml, "head");
var adrbody = xmlGetAddress (adropml, "body");
emojify.setConfig ({
img_dir: "http://fargo.io/code/emojify/images/emoji",
});
xmlVisit (adrbody, function (adrx, level, path) {
var textatt = xmlGetTextAtt (adrx);
var s = emojify.replace (textatt);
if (s != textatt) {
xmlSetAttribute (adrx, "text", s)
console.log (s);
}
return (true);
});
var serializer = new XMLSerializer ();
opmltext = serializer.serializeToString (xstruct);
console.log (opmltext);
return (opmltext);
}
@scripting
Copy link
Author

I want to write a routine that emojifies an OPML file.

It takes OPML text as input, parses it, walks the whole structure, and emojifies the text, and then replaces the original text with the new text.

At the end of the traversal, it re-serializes the XML text, and returns it.

However, I get an error at the crucial point, the call to serializeToString.

Uncaught TypeError: Failed to execute 'serializeToString' on 'XMLSerializer': Invalid node value.

Here's a file that contains good example text.

http://liveblog.co/users/davewiner/myNoteblog.opml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment