0

I'm trying to build a XML node with xml2js. When I add html tags wrapped in cdata, it creates a separate node as shown below. And if I wrap the CDATA inside "<>" the value inside gets undefined. Basically I'm trying to create XML documents from which I can take the node values and show it in the HTML documents using jQuery/Angular. And those node must be able to contain HTML tags

var xml2js = require('xml2js');

var parser = new xml2js.Parser({
    explicitArray: false
});
var builder = new xml2js.Builder({
    cdata: true
});


var test = "<parent>![CDATA[Hey There! <span> Buddy.</span>]]</parent>"

parser.parseString(test, function(err, result) {
    var xml = builder.buildObject(result)
    console.log(xml)
});
//Output
/*
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
  ![CDATA[Hey There! ]]
  <span> Buddy.</span>
</parent>
*/


var test2 = "<parent><![CDATA['Hey There! <span> Buddy.</span>']]></parent>";

parser.parseString(test, function(err, result) {
    var xml = builder.buildObject(result)
    console.log(xml)
});
//Output : 
/*
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent><![CDATA[undefined]]></parent>
*/

2 Answers 2

2

Your first test was almost correct. You actually needed the < > symbols around the CDATA content.

This code:

var xml2js = require('xml2js');

var parser = new xml2js.Parser({
    explicitArray: false
});
var builder = new xml2js.Builder({
    cdata: true
});

var test = "<parent><![CDATA[Hey There! <span> Buddy.</span>]]></parent>";

parser.parseString(test, function(err, result) {
    var xml = builder.buildObject(result);
    console.log(xml);
});

Produces this output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent><![CDATA[Hey There! <span> Buddy.</span>]]></parent>
Sign up to request clarification or add additional context in comments.

6 Comments

Hey @trevor, Look at the test number 2. Its same as yours but I still get the value as undefined.
I used your code and tested it. My output is <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <parent><![CDATA[undefined]]></parent>
Your test number 2 was the same as mine except for the single quotes you used within the CDATA section (which don't really affect anything) and the fact that you were passing your "test" variable to the parseString function when you should have been passing "test2" instead. The code I posted definitely produces the result I posted. What version of xml2js are you using? I am using version 0.4.8 in my code.
Also note that there was a fix for CDATA tags authored only 24 days ago: github.com/Leonidas-from-XIV/node-xml2js. I would make sure xml2js is listed in your devDependencies and try running npm update.
Wow man. Thanks a lot. I updated xml2js and it worked. Will up vote when I'm eligible.
|
0

It could be made without the Parser. _ helps to handle the inner text inside tags.

const xml2js = require('xml2js');

const builder = new xml2js.Builder({
    cdata: true
});

const test = "Hey There! <span> Buddy.</span>";

const xml = builder.buildObject({
   parent: {
     _: test,
   }
});

console.log(xml);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.