Is there an example of how to include xml2js in the browser page?
Per xml2js https://github.com/Leonidas-from-XIV/node-xml2js/issues/215 :
"with the recent changes, xml2js should be working fine in the browser.
A simple include does not seem to work :
<script type="text/javascript" src="/dist/xml2js.js"></script>
as it generates the error
Uncaught ReferenceError: require is not defined
Is it a matter of running browserify to create a single file to include?
Theoretically browserify
ing it should work as discussed at https://github.com/Leonidas-from-XIV/node-xml2js/issues/277 ( "pietersv commented on Nov 2, 2017" ).
Quoting that:
Add the following two scripts to package.json and run them after running npm test:
"browserify": "browserify ./xml2js.js -s xml2js -o ./xml2js.js",
"minify": "cat ./xml2js.js | uglifyjs > ./xml2js.min.js"
in HTML
<script type="text/javascript" src="/path/to/xml2js.min.js"></script>
<script>
var xml = '<?xml version="1.0" encoding="UTF-8"?><note>Lorem ipsum dolor sit amet...</note>'
var obj = xml2js.parseString(xml, function(err, res) { ... })
</script>
However as at today (30.7.18) that is not working with the current version (0.4.19) as stated in the follow-up comment (by me!).
Instead I used https://www.npmjs.com/package/xml-js and browserfied it (in package.json
is:
"buildxml2js": "./node_modules/.bin/browserify ./node_modules/xml-js/dist/xml-js.js -o ./public/lib/xml-js.js")
and it works a charm. After building it as ./public/lib/xml-js.js
dir add this to the html:
<script type="text/javascript" src="/lib/xml-js.js"></script>
And then call with:
xml2json(the_xml)
and
json2xml(the_json)
Sorry this doesn't actually solve your specific question, however it provides a viable work-around.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.