Reputation: 830
I am using xmlbuilder2 to create an XML document from a JS object.
This works just fine:
const { create } = require('xmlbuilder2');
const obj = {
root: {
'@att': 'val',
foo: {
bar: 'foobar'
},
baz: {}
}
};
const doc = create(obj);
const xml = doc.end({ prettyPrint: true });
console.log(xml);
However, I want to get the following XML:
<?xml version="1.0"?>
<root att="val">
<foo>
<bar myattr="hello">foobar</bar>
</foo>
<baz/>
</root>
How do I set both the attribute and content for the bar
element?
I did not find any examples online, I've tried using @text
but it didn't work.
Thanks!
Upvotes: 2
Views: 1059
Reputation: 830
I figured it out (with the help of Tomalak!) that the correct way is to do:
const { create } = require('xmlbuilder2');
const obj = {
root: {
'@att': 'val',
foo: {
bar: {"@myattr":"hello", "#":'foobar'}
},
baz: {}
}
};
const doc = create(obj);
const xml = doc.end({ prettyPrint: true });
console.log(xml);
Upvotes: 1