How do I "demote" an XML Node (move it to the lastChild position of the previous sibling)?

How do I "demote" an XML Node (move it to the lastChild position of the previous sibling)?

This is what I have so far:
 
  1. <!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">
  2.       <head>
  3.             <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  4.             <title>Parse Test</title>
  5.             <script type="text/javascript" src="jQuery-1.9.1.js"></script>
  6.       </head>
  7.       <body>
  8.             <div id="outputDiv">Output DIV<br/></div>
  9.             <script>
  10.                   alert("begin");
  11.                   var xml =
  12.                   "<rss>Test RSS" +
  13.                         "<channel>Channel Info</channel>" + 
  14.                         "<channel>Channel 01" +
  15.                               "<episode>Episode 01" +
  16.                                     "<break>First Break</break>" +
  17.                               "</episode>" +
  18.                         "</channel>" +
  19.                         "<channel>Channel 02</channel>"  +
  20.                         "<episode>Episode 02 - demote</episode>"  +
  21.                         "<channel>Channel 03</channel>" +
  22.                   "</rss>";
  23.                   var xmlDoc = $.parseXML(xml);
  24.                   var $xml = $(xmlDoc);
  25.                   var iString;
  26.                   traverse($xml);
  27.                   function traverse(tree) {
  28.                         var fontColor;
  29.                         $(tree).contents().each(function() {
  30.                               var p = this.parentNode.nodeName;
         var n = this.nodeName;
         var hierarchyLevel = $(this).parents().length;
         
         iString = repeat("&nbsp;&nbsp;&nbsp;&nbsp;", 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());
         
         } */
  31.      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)
  32.       // node value: $(this).text() or this.nodeValue
          $("#outputDiv").append(iString + "&lt;" + this.nodeName + " level=" + hierarchyLevel + "&gt;"); // tag
          
          $("#outputDiv").append("<font color=" + fontColor + ">" + $(this).text() + "</font>"); // text contents
  33.      } else {
         
          fontColor = "black";
  34.       console.log('parent-nodeName: ' + p);
          console.log('currnt-nodeName: ' + n);
          console.log('tag name: ' + this.nodeName);
          
          console.log(hierarchyLevel)
          
          $("#outputDiv").append(iString + "&lt;" + this.nodeName + " level=" + hierarchyLevel + "&gt;" + "<br/>");
  35.       traverse(this);
         
         }
         
         if (this.nodeType == 3) { // text node
  36.       $("#outputDiv").append("&lt;" + "\/" + this.nodeName + "&gt;" + "<br/>");
  37.      } else {
         
          $("#outputDiv").append(repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $(this).parents().length) + "&lt;" + "\/"
           + this.nodeName + "&gt;" + "<br/>");
          
         }
        
        });
        
        return tree;
       
       }
       
       function repeat(str, times) {
       
        return new Array(times + 1).join(str);
       
       }
       
       alert("end");
       
      </script>
  38.  </body>
  39. </html>
Any ideas on how to move it?
 
Thanks,
 
Doug