How do I "demote" an XML Node (move it to the lastChild position of the previous sibling)?
This is what I have so far:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
- <title>Parse Test</title>
- <script type="text/javascript" src="jQuery-1.9.1.js"></script>
- </head>
- <body>
- <div id="outputDiv">Output DIV<br/></div>
- <script>
- alert("begin");
- var xml =
- "<rss>Test RSS" +
- "<channel>Channel Info</channel>" +
- "<channel>Channel 01" +
- "<episode>Episode 01" +
- "<break>First Break</break>" +
- "</episode>" +
- "</channel>" +
- "<channel>Channel 02</channel>" +
- "<episode>Episode 02 - demote</episode>" +
- "<channel>Channel 03</channel>" +
- "</rss>";
- var xmlDoc = $.parseXML(xml);
- var $xml = $(xmlDoc);
- var iString;
- traverse($xml);
- function traverse(tree) {
- var fontColor;
- $(tree).contents().each(function() {
- var p = this.parentNode.nodeName;
var n = this.nodeName;
var hierarchyLevel = $(this).parents().length;
iString = repeat(" ", hierarchyLevel);
// Working out the demote function
/* if (($(this).text().indexOf("demote") != -1) && (this.nodeType ==3)) {
$("#outputDiv").append(iString + this.parentNode.nodeName + "<br/>");
alert($(this).text());
} */
- if (($(this).text().indexOf("demote") != -1) && (this.nodeType ==1) && ($(this).parents().length > 0)) {
// alert($(this).parents().length);
// alert($(this).text());
// $(this).empty();
// $(this).remove();
// traverse(tree);
}
if (this.nodeType == 3) { // text node
fontColor = "green";
console.log('parent-nodeName: ' + p);
console.log('currnt-nodeName: ' + n);
console.log('text node value: ' + $(this).text());
console.log($(this).parents().length)
- // node value: $(this).text() or this.nodeValue
$("#outputDiv").append(iString + "<" + this.nodeName + " level=" + hierarchyLevel + ">"); // tag
$("#outputDiv").append("<font color=" + fontColor + ">" + $(this).text() + "</font>"); // text contents
- } else {
fontColor = "black";
- console.log('parent-nodeName: ' + p);
console.log('currnt-nodeName: ' + n);
console.log('tag name: ' + this.nodeName);
console.log(hierarchyLevel)
$("#outputDiv").append(iString + "<" + this.nodeName + " level=" + hierarchyLevel + ">" + "<br/>");
- traverse(this);
}
if (this.nodeType == 3) { // text node
- $("#outputDiv").append("<" + "\/" + this.nodeName + ">" + "<br/>");
- } else {
$("#outputDiv").append(repeat(" ", $(this).parents().length) + "<" + "\/"
+ this.nodeName + ">" + "<br/>");
}
});
return tree;
}
function repeat(str, times) {
return new Array(times + 1).join(str);
}
alert("end");
</script>
- </body>
- </html>
Any ideas on how to move it?
Thanks,
Doug