`N/xml` In NetSuite is such a pain...
# suitescript
a
N/xml
In NetSuite is such a pain...
s
JSONIX
a
@stalbert Do you use in multiple regular projects? is that as stable and maintained as moment or lodash?
s
it's not as maintained as moment or lodash because it's a much more niche use case than either of those two. I have used it in multiple projects and it's proven to be the most flexible JS<->XML library I've found. I do with there was continued development on it but I did just use it again in a project a few weeks ago.
after reviewing a variety of JS libs for doing this, many are quite limited and I much preferred having one library for this rather than a messy situation of "use lib A if this, lib B if that, or lib C if something else"
JSONIX is more complex than the others but therein lies it's flexibility - you define declarative mappings between JS and XML that work bidirectionally, and it supports expected issues like attributes vs. elements, type restrictions, etc.
a
Thanks.
s
Almost always, I've used the generator it comes with whereby you provide XSD definitions of your XML and it generates the JS mappings for you.
most recently, I used it to call a SOAP web service, and is far more stable than simple string manipulation to create proper SOAP messages (which I see suggested all to often here :\)
the original developer doesn't seem very active on the project, but as long as it remains the most reliable library as-is I don't see a reason not to use it. https://github.com/highsource/jsonix
I think I did have to tweak it a bit to make it work correctly in SS 2.1
a
Any special trick to make it work with SS 2.0, or it just works flawlessly?
s
I don't remember what the SS2.0 experience was like. I do recall the issue wasn't fundamental - it was more just tweaking the module system detection boilerplate code.
NS provides a somewhat broken AMD environment which breaks some common module detection techniques
a
Do you have any sample code of unmarshalling?
s
I encapsulated all interactions with this SOAP service into a class, but the unmarshalling line is as simple as
this.unmarshaller.unmarshalString(response.body)
where
response
is of type
ClientResponse
from the https module.
note that only marshalString/unmarshalString work in SS, but that's also all I need.
a
That also all I need...
Copy code
var context = new Jsonix.Context([PO]);
Did you create a mapping manually?
s
I generated my mapping with the included java jar
from existing XSDs
a
DO you remember how did you require or define PO to be available for jsonix context?
Copy code
var context = new jsonix.Context([PO]);
?
s
Copy code
import { omscom } from './omscom'
import { Jsonix } from './jsonix3.0.0'
omscom
is my mapping file
the class creates a context and marshaller unmarshaller
lol just noticed I typoed the 'marhsaller' property, will have to fix that, though it's not visible outside the class to not such a big deal I suppose.
a
I think I'm missing something... I'm using jsonix within a module...
Untitled
I not understanding where to import or include by require or define the mapping...
s
jsonix-min didn't work for me due to NS AMD
a
ohhh
s
I have dom.js dom-parser.js entities.js jsonix3.0.0.js omscom.js and sax.js in my project
looks scary, but these are just stuff inside jsonix-min
and it's one-and-done - I should never have to futz with it again until SS2.2 breaks something else
a
I'm still getting errors when doing var context = new jsonix.Context([XML_MAP]);
XML_MAP is just an OBJECT defined within my module...
org.mozilla.javascript.EcmaError: TypeError: org.mozilla.javascript.Undefined@2dc7122 is not a function, it is undefined. (/SuiteScripts/Module.js#289)
Pass that error with var context = new jsonix.Jsonix.Context([XML_MAP]); Now: ReferenceError: "XMLHttpRequest" is not defined.
I will end up writing my own thing for this...
s
Yes, I had to wrestle a few items, but doing so ended up being less work than writing my own XML<->JS library by probably a thousand hours.