xml-typescript
xml-typescript copied to clipboard
Creating mixed content
I am trying to model classes that can support mixed content. From the example at https://www.npmjs.com/package/js2xmlparser I could do this with
var js2xmlparser = require("js2xmlparser");
var obj = {
"phone": [
{
"@": {
"type": "home"
},
"#": "123-555-4567"
},
{
"@": {
"type": "cell"
},
"#": "890-555-1234"
}
],
"email": "[email protected]"
};
console.log(js2xmlparser.parse("person", obj));
which gives me an xml
<?xml version='1.0'?>
<person>
<phone type='home'>123-555-4567</phone>
<phone type='cell'>890-555-1234</phone>
<email>[email protected]</email>
</person>
How would I go about doing this using xml-decorators?
@sshivananda Unfortunately this is not possible 🤔There would be something like a @Value
annotation needed to mark a property as the value, of course. Basically what #
does for js2xmlparser
.
I am trying to add support for this and would to happy to raise a PR if I succeed - do you have pointers on where I could start
Hey @sshivananda, any help is appreciated 👍 So, here we go: The schema is processed in the XMLElement
model. For instance the attributes (which are defined via @
) are set here: https://github.com/RobinBuschmann/xml-typescript/blob/master/lib/models/XMLElement.ts#L163
Hope this helps!
Robin, I created a modified version which offers a @XMLText decorator (with a single option required). It creates the missing #
key inside the schema consumed by js2xmlparser
.
My local tests were positive.
Are you interested in a PR?
@rzacherl Yes, for sure 👍 Just create the PR so that I can review it