Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.87k stars 601 forks source link

Build XML with raw string #548

Open jbpin opened 4 years ago

jbpin commented 4 years ago

Hi,

I try to use a #raw key but I get an error

Error: The root node has no parent. Use doc() if you need to get the document object.
at XMLElement.module.exports.XMLNode.up (.../node_modules/xmlbuilder/lib/XMLNode.js:455:15)

Here is a code to reproduce

const {Builder} = require('xml2js');
const obj = {
  data: {'#raw' : "this is a raw string"}
}
let result = new Builder().buildObject(obj);

Do you have any idea how I can use a raw string with xml2js? Apparently everything use txt. Do you intend to support xmlbuilder's decorators?

Omega-Ariston commented 4 years ago
  const {Builder} = require('xml2js');
  const obj = {
    data: "this is a raw string"
  }
  let result = new Builder({ cdata: true }).buildObject(obj);

With xml2js, you can use the attribute 'cdata' directly. For more detail, take a look at this builder.coffee file :)
https://github.com/Leonidas-from-XIV/node-xml2js/blob/8fc5b926846cd4ef9a2dbccd411705e0c110a708/src/builder.coffee#L47-L50