I’m reading an xml file line by line:
var lines = $(xml).text().split("\n");
$.each(lines, function(n, elem) {
console.log(elem);
});
As a result, this only outputs the content between the tags, not the xml lines themselves. I’ve been looking for what could “trim” the lines from the enclosing tags, but haven’t been successful at it.
When you do
$(xml), you construct a node tree from the stringxml. Then, withtext(), you treat this node tree as HTML and obtain the text contents of each tag.e.g.
(There are more examples of this on the jQuery
.text()documentation page; you should read the documentation for every function you use.)But there’s no need here for you to be constructing a node tree only to attempt to re-obtain the source XML after-the-fact. In fact,
.html()does not work on XHTML or XML.Instead, just use
xmllike you would any other string: