Problem in printing angle brackets using the xml-builder node module











up vote
1
down vote

favorite












I am creating an xml file using "xml-builder" node module. But when I tried to write angle brackets ("<" or ">"), I got characters like "<" and ">". The code is as follows:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello").up();
xml.ele('name',"<Hello> "+name+" </Hello>").up();
xml.end({ pretty: true });
console.log(xml.toString())


The output is as follows:



<Slides>
<props>Hello</props>
<name>&lt;Hello&gt; ABC &lt;/Hello&gt;</name>
</Slides>


What should I do to get < or > printed instead of &lt; or &gt; ?










share|improve this question
























  • Replace them with &lt; etc...
    – Zohir Salak
    Nov 9 at 4:15






  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:12










  • please review my answer @KushagraSinha , leave a comment if it doesn't work or accept it if it does
    – mihai
    Nov 10 at 9:57















up vote
1
down vote

favorite












I am creating an xml file using "xml-builder" node module. But when I tried to write angle brackets ("<" or ">"), I got characters like "<" and ">". The code is as follows:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello").up();
xml.ele('name',"<Hello> "+name+" </Hello>").up();
xml.end({ pretty: true });
console.log(xml.toString())


The output is as follows:



<Slides>
<props>Hello</props>
<name>&lt;Hello&gt; ABC &lt;/Hello&gt;</name>
</Slides>


What should I do to get < or > printed instead of &lt; or &gt; ?










share|improve this question
























  • Replace them with &lt; etc...
    – Zohir Salak
    Nov 9 at 4:15






  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:12










  • please review my answer @KushagraSinha , leave a comment if it doesn't work or accept it if it does
    – mihai
    Nov 10 at 9:57













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am creating an xml file using "xml-builder" node module. But when I tried to write angle brackets ("<" or ">"), I got characters like "<" and ">". The code is as follows:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello").up();
xml.ele('name',"<Hello> "+name+" </Hello>").up();
xml.end({ pretty: true });
console.log(xml.toString())


The output is as follows:



<Slides>
<props>Hello</props>
<name>&lt;Hello&gt; ABC &lt;/Hello&gt;</name>
</Slides>


What should I do to get < or > printed instead of &lt; or &gt; ?










share|improve this question















I am creating an xml file using "xml-builder" node module. But when I tried to write angle brackets ("<" or ">"), I got characters like "<" and ">". The code is as follows:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello").up();
xml.ele('name',"<Hello> "+name+" </Hello>").up();
xml.end({ pretty: true });
console.log(xml.toString())


The output is as follows:



<Slides>
<props>Hello</props>
<name>&lt;Hello&gt; ABC &lt;/Hello&gt;</name>
</Slides>


What should I do to get < or > printed instead of &lt; or &gt; ?







javascript node.js npm xml-builder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 13:06









mihai

23.1k73968




23.1k73968










asked Nov 9 at 4:11









Kushagra Sinha

164




164












  • Replace them with &lt; etc...
    – Zohir Salak
    Nov 9 at 4:15






  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:12










  • please review my answer @KushagraSinha , leave a comment if it doesn't work or accept it if it does
    – mihai
    Nov 10 at 9:57


















  • Replace them with &lt; etc...
    – Zohir Salak
    Nov 9 at 4:15






  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:12










  • please review my answer @KushagraSinha , leave a comment if it doesn't work or accept it if it does
    – mihai
    Nov 10 at 9:57
















Replace them with &lt; etc...
– Zohir Salak
Nov 9 at 4:15




Replace them with &lt; etc...
– Zohir Salak
Nov 9 at 4:15




1




1




Still not getting desired output
– Kushagra Sinha
Nov 9 at 5:12




Still not getting desired output
– Kushagra Sinha
Nov 9 at 5:12












please review my answer @KushagraSinha , leave a comment if it doesn't work or accept it if it does
– mihai
Nov 10 at 9:57




please review my answer @KushagraSinha , leave a comment if it doesn't work or accept it if it does
– mihai
Nov 10 at 9:57












2 Answers
2






active

oldest

votes

















up vote
0
down vote













There is an npm module decode-html that will handle the same use case as your.



var decode = require('decode-html');

console.log(decode('&lt;div class="hidden"&gt;NON&amp;SENSE&apos;s&lt;/div&gt;'));
// -> '<div class="hidden">NON&SENSE's</div>'





share|improve this answer

















  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:21


















up vote
0
down vote













The problem is that is you are attempting to create a child element in an incorrect way, by passing some xml in the value field of xml.ele. The module is correctly escaping your angle brackets.



What you need to do is create another element named Hello and append it to the name element. You can do this by either chaining your .ele calls or using their return values.



Here is the correct code:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello");
xml.ele('name')
.ele("Hello", name);
xml.end({ pretty: true });
console.log(xml.toString())


Output:



<Slides>
<props>Hello</props>
<name>
<Hello>ABC</Hello>
</name>
</Slides>





share|improve this answer























  • Sir, actually I have to create "cdata" section in the xml file. Ex: "<notestext><![CDATA[{Notes Text}]]></notestext>". How to create that cdata section?
    – Kushagra Sinha
    Nov 10 at 10:36










  • See github.com/oozcitak/xmlbuilder-js/wiki#cdata-nodes or ask a different question
    – mihai
    Nov 10 at 10:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219765%2fproblem-in-printing-angle-brackets-using-the-xml-builder-node-module%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













There is an npm module decode-html that will handle the same use case as your.



var decode = require('decode-html');

console.log(decode('&lt;div class="hidden"&gt;NON&amp;SENSE&apos;s&lt;/div&gt;'));
// -> '<div class="hidden">NON&SENSE's</div>'





share|improve this answer

















  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:21















up vote
0
down vote













There is an npm module decode-html that will handle the same use case as your.



var decode = require('decode-html');

console.log(decode('&lt;div class="hidden"&gt;NON&amp;SENSE&apos;s&lt;/div&gt;'));
// -> '<div class="hidden">NON&SENSE's</div>'





share|improve this answer

















  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:21













up vote
0
down vote










up vote
0
down vote









There is an npm module decode-html that will handle the same use case as your.



var decode = require('decode-html');

console.log(decode('&lt;div class="hidden"&gt;NON&amp;SENSE&apos;s&lt;/div&gt;'));
// -> '<div class="hidden">NON&SENSE's</div>'





share|improve this answer












There is an npm module decode-html that will handle the same use case as your.



var decode = require('decode-html');

console.log(decode('&lt;div class="hidden"&gt;NON&amp;SENSE&apos;s&lt;/div&gt;'));
// -> '<div class="hidden">NON&SENSE's</div>'






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 4:57









front_end_dev

1,3151511




1,3151511








  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:21














  • 1




    Still not getting desired output
    – Kushagra Sinha
    Nov 9 at 5:21








1




1




Still not getting desired output
– Kushagra Sinha
Nov 9 at 5:21




Still not getting desired output
– Kushagra Sinha
Nov 9 at 5:21












up vote
0
down vote













The problem is that is you are attempting to create a child element in an incorrect way, by passing some xml in the value field of xml.ele. The module is correctly escaping your angle brackets.



What you need to do is create another element named Hello and append it to the name element. You can do this by either chaining your .ele calls or using their return values.



Here is the correct code:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello");
xml.ele('name')
.ele("Hello", name);
xml.end({ pretty: true });
console.log(xml.toString())


Output:



<Slides>
<props>Hello</props>
<name>
<Hello>ABC</Hello>
</name>
</Slides>





share|improve this answer























  • Sir, actually I have to create "cdata" section in the xml file. Ex: "<notestext><![CDATA[{Notes Text}]]></notestext>". How to create that cdata section?
    – Kushagra Sinha
    Nov 10 at 10:36










  • See github.com/oozcitak/xmlbuilder-js/wiki#cdata-nodes or ask a different question
    – mihai
    Nov 10 at 10:39















up vote
0
down vote













The problem is that is you are attempting to create a child element in an incorrect way, by passing some xml in the value field of xml.ele. The module is correctly escaping your angle brackets.



What you need to do is create another element named Hello and append it to the name element. You can do this by either chaining your .ele calls or using their return values.



Here is the correct code:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello");
xml.ele('name')
.ele("Hello", name);
xml.end({ pretty: true });
console.log(xml.toString())


Output:



<Slides>
<props>Hello</props>
<name>
<Hello>ABC</Hello>
</name>
</Slides>





share|improve this answer























  • Sir, actually I have to create "cdata" section in the xml file. Ex: "<notestext><![CDATA[{Notes Text}]]></notestext>". How to create that cdata section?
    – Kushagra Sinha
    Nov 10 at 10:36










  • See github.com/oozcitak/xmlbuilder-js/wiki#cdata-nodes or ask a different question
    – mihai
    Nov 10 at 10:39













up vote
0
down vote










up vote
0
down vote









The problem is that is you are attempting to create a child element in an incorrect way, by passing some xml in the value field of xml.ele. The module is correctly escaping your angle brackets.



What you need to do is create another element named Hello and append it to the name element. You can do this by either chaining your .ele calls or using their return values.



Here is the correct code:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello");
xml.ele('name')
.ele("Hello", name);
xml.end({ pretty: true });
console.log(xml.toString())


Output:



<Slides>
<props>Hello</props>
<name>
<Hello>ABC</Hello>
</name>
</Slides>





share|improve this answer














The problem is that is you are attempting to create a child element in an incorrect way, by passing some xml in the value field of xml.ele. The module is correctly escaping your angle brackets.



What you need to do is create another element named Hello and append it to the name element. You can do this by either chaining your .ele calls or using their return values.



Here is the correct code:



let builder = require('xmlbuilder', { encoding: 'utf-8' });
let name = "ABC";
let xml = builder.create('Slides');
xml.ele('props',"Hello");
xml.ele('name')
.ele("Hello", name);
xml.end({ pretty: true });
console.log(xml.toString())


Output:



<Slides>
<props>Hello</props>
<name>
<Hello>ABC</Hello>
</name>
</Slides>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 13:25

























answered Nov 9 at 13:11









mihai

23.1k73968




23.1k73968












  • Sir, actually I have to create "cdata" section in the xml file. Ex: "<notestext><![CDATA[{Notes Text}]]></notestext>". How to create that cdata section?
    – Kushagra Sinha
    Nov 10 at 10:36










  • See github.com/oozcitak/xmlbuilder-js/wiki#cdata-nodes or ask a different question
    – mihai
    Nov 10 at 10:39


















  • Sir, actually I have to create "cdata" section in the xml file. Ex: "<notestext><![CDATA[{Notes Text}]]></notestext>". How to create that cdata section?
    – Kushagra Sinha
    Nov 10 at 10:36










  • See github.com/oozcitak/xmlbuilder-js/wiki#cdata-nodes or ask a different question
    – mihai
    Nov 10 at 10:39
















Sir, actually I have to create "cdata" section in the xml file. Ex: "<notestext><![CDATA[{Notes Text}]]></notestext>". How to create that cdata section?
– Kushagra Sinha
Nov 10 at 10:36




Sir, actually I have to create "cdata" section in the xml file. Ex: "<notestext><![CDATA[{Notes Text}]]></notestext>". How to create that cdata section?
– Kushagra Sinha
Nov 10 at 10:36












See github.com/oozcitak/xmlbuilder-js/wiki#cdata-nodes or ask a different question
– mihai
Nov 10 at 10:39




See github.com/oozcitak/xmlbuilder-js/wiki#cdata-nodes or ask a different question
– mihai
Nov 10 at 10:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219765%2fproblem-in-printing-angle-brackets-using-the-xml-builder-node-module%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud