untitled notebook

node v14.20.1
version: 1.0.0
endpointsharetweet
const xml2js = require('xml2js') const input = { RootElement: { id: "1234", name: "Foo", children: [ { id: "5678", name: "Bar", props: [ { name: "Baz", code: "ABC" }, { name: "Qux", code: "DEF" } ] } ] } } const prefixProps = (pre) => (obj) => Array.isArray (obj) ? obj .map (prefixProps (pre)) : Object (obj) === obj ? Object .fromEntries (Object .entries (obj) .map (([k, v]) => [ `${pre}:${k}`, prefixProps (pre) (v) ]) ) : obj const addNamespace = (pre, ns) => (obj) => Object .fromEntries (Object .entries (prefixProps (pre) (obj)) .map (([k, v]) => [ // TODO: syntactic cleanup k, ( ( Array .isArray (v) ? v = {$: {[`xmlns:${pre}`]: ns}, _: v} : Object(v) === v ? v.$ = {[`xmlns:${pre}`]: ns} : v = {$: {[`xmlns:${pre}`]: ns}, _: v} ), v ) ]) ) const converted = {RootElement: addNamespace ('ns1', 'http://my.namespace') (input.RootElement)} console .log (converted) console .log (JSON.stringify (converted, null, 4)) const builder = new xml2js.Builder({headless: true}); const xml = builder.buildObject (converted) console .log (xml)
Loading…

no comments

    sign in to comment