Using SDK to develop addon that requires XML parsing

I’m working through the SDK tutorials to evaluate this as a way of refactoring my company’s addon.

We do a lot of client-side XML parsing, so it’s essential to be able to parse XML, evaluate XPath expressions, and save the results to a local file.

So far the SDK’s abilities in this regard seem to be severely restricted, but it’s possible I don’t have all the information, or have not yet found the most up to date information on this.

Currently I’m stymied by a problem instantiating an XPathEvalator object. I’m using code taken directly from Mozilla tutorials such as Using XPath. This code will work in Scratchpad, but when I run FDE using JPM, I get this error:

XPathEvaluator is not defined

A helpful user on StackOverflow pointed out that this is likely because this object is sandboxed out of JPM.

But if I try to instantiate using Cc, I encounter the same error described here.

A solution to this immediate problem would be welcome, but my bigger question is about the viability of using SDK and JPM to develop my addon. Am I better off not doing it this way?

I’ll add that I’m having trouble distinguishing which Mozilla articles are out of date and which ones are current, so some advice along those lines would be welcome also!

It’s possible those objects only work on a window context, which is why it will be difficult to run them using the SDK. You could try creating an HTML document and then loading those objects there. There’s some info about XPath and XML parsing here. In general it should be possible to use those objects with Cc, but it sounds like XPath changed at some point and it’s no longer possible.

So, to be clear, do you mean that it is impossible to develop an SDK addon that does client-side XML parsing? What do you recommend instead, doing a “legacy” type overlay addon? Do you know if there are plans to add this functionality to the SDK at any time in the future?

Thanks!

It’s not impossible, it just requires some workarounds like I mentioned. There are some plans to improve the extensions API, but those are still underway. For now, you have to either try the workarounds I suggested, or go with the other two options (bootstrapped extension or XUL overlay extension). See this post for an overview, if this is still confusing.

I have no idea what XPath is and all, so excuse me if I’m talking gibberish.
But couldn’t you hoist the XPathEvaluator from a window?
Looking at the tutorial you pass a DOM node, so you must have a window, no?

I know that using DOMParser you can parse a string of html and then navigate it with document.getElementById querySelector etc, so you will probably get XPathEvaluator too, so check out DOMParser over here: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser and from sandbox this is accessed with nsIDOMParser: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMParser

This code on github shows using nsIDOMXPathEvaluator and nsIDOMParser along with nsIDOMSerializer and nsIXSLTProcessor very interesting stuff but I think all you need is right here:

Actually this guy here users nsIDOMXPathEvaluator along with nsIDOMParser this will probably help more:

Thanks! However I believe the git projects you reference are out of date. I tried to do the same thing and got errors, so there doesn’t appear to be a working sandbox nsIDOMXPathEvaluator since FF 32. The stacktrace I got references Promise-backend.js, so it’s something that has not been implemented (though it worked in the past, so it’s a fair bet it’s out for good).

Thats interesting, they deprecated it without any alternative?

Worst case, you can use Services.appshell.hiddenDOMWindow for its functionality.