2

I have searched the documentation at https://github.com/polotek/libxmljs/wiki but fails to find out how to create plain text nodes.

Here is my node.js code:

var libxmljs = require('libxmljs');
var xmlStr   = '<div><p>bar <strong>foo</strong></p></div>';
var doc      = libxmljs.parseXmlString(xmlStr);

// Create a normal extra node
doc.get('/div/p').node('span', 'foobar');

doc.toString() now results in:

<?xml version="1.0" encoding="UTF-8"?>
<div>
  <p>bar <strong>foo</strong><span>foobar</span></p>
</div>

But what if I wanted:

<?xml version="1.0" encoding="UTF-8"?>
<div>
  <p>bar <strong>foo</strong> foobar</p>
</div>

I am aware that I can set the content of a node with .text(), so I COULD do this by getting the string content of <p>, append it as a string and re-set it with .text() again. That is not what I need. I need to create a text node in the DOM.

Is this not possible, or am I just to bad at reading the docs?

Lilleman
  • 7,392
  • 5
  • 27
  • 36
  • Thanks to setec@feenode IRC I found ot that the connection in libxml I need is this: http://xmlsoft.org/html/libxml-tree.html#xmlNewDocText - maybe libxmljs does not support this? – Lilleman Oct 10 '14 at 08:49
  • It seems in the tests in libxmljs it only supports resolving and showing info about text nodes, but not create them: https://github.com/polotek/libxmljs/blob/master/test/text_node.js – Lilleman Oct 10 '14 at 13:11

0 Answers0