xml-js
xml-js copied to clipboard
Option to extract attributes without 'attr' key, on element level
i.e.
<SomeThing SomeAttr="VALUE">CONTENT</SomeThing>
=>
{
SomeThing : {
SomeAttr: "VALUE"
_text: "CONTENT"
}
}
instead of
{
SomeThing : {
_attrs: {
SomeAttr: "VALUE"
},
_text: "CONTENT"
}
}
or how to implement this effectively?
maybe allow transformations like postPlaceFn: (key, ctx, default) => (key == '_attributes') ? ctx : ctx[key] or similar?
Hi, is this implemented in the actual versión?
attrsAsProps: true
Any updates on this?
I created a quick and dirty PR to do this https://github.com/nashwaan/xml-js/pull/195
Would be great to see this implemented
I did some tests, and this seems to work 😮
xml2js(xml, {
compact: true,
alwaysArray: true,
attributesFn: (attributes, parent) => {
Object.assign(parent, attributes);
return undefined;
}
})
EDIT: Hmmm, maybe there's no data if the tag has no children 😕
<SomeThing SomeAttr="VALUE">CONTENT</SomeThing>
may be converted to json
{
"SomeThing": {
"-SomeAttr": "VALUE",
"#text": "CONTENT"
},
"#omit-xml-declaration": "yes"
}
Removing the attributes parent node would be useful for size reduction. In my case a json file goes:
- Before: 766 lines / 14KB
- After: 532 lines / 10KB