JavaScript.CoolDev.Com Support Forums

Any questions related to JavaScript menu and JavaScript tree menu by Javascript.CoolDev.Com

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

cool js tree output in XML format

 
Post new topic   Reply to topic    JavaScript.CoolDev.Com Forum Index -> COOLjsTree
Author Message
ameelin



Joined: 15 Jul 2003
Posts: 1
Location: daytona beach

PostPosted: Tue Jul 15, 2003 8:43 pm    Post subject: cool js tree output in XML format Reply with quote

How can I get the cool js tree heirarchical output in XML format by clicking on a link? Has anybody done that? Would appreciate if you could share...
Back to top
AlexKunin
Developer


Joined: 03 Jan 2003
Posts: 1191

PostPosted: Wed Jul 16, 2003 9:07 pm    Post subject: Reply with quote

Sorry, not very clear... Do you want to generate XML from existing tree structure? Or vice versa?
Back to top
Guest






PostPosted: Thu Jul 17, 2003 11:47 am    Post subject: Reply with quote

To generate XML from existing tree structure.
Back to top
AlexKunin
Developer


Joined: 03 Jan 2003
Posts: 1191

PostPosted: Thu Jul 17, 2003 8:18 pm    Post subject: Reply with quote

Not sure if I understand your aims, but here is a sample. Function "toXML" probably is what you want. You can paste whole code into new .html file and test it with your browser.
Code:
<script type="text/javascript">
var TREE_NODES = [
   ['Scripts', null, null,
           ['COOLjsTree', "http://javascript.cooldev.com/scripts/cooltree/", null],
           ['COOLjsMenu', "http://javascript.cooldev.com/scripts/coolmenu/", null],
   ],
   ['Online help', null, null,
      ['COOLjsTree', "http://javascript.cooldev.com/scripts/cooltree/docs/", null],
      ['COOLjsMenu', "http://javascript.cooldev.com/scripts/coolmenu/docs/", null],
   ],
   ['Download', null, null,
      ['Free', null, null],
      ['Order', null, null],
   ]
];

function toXML(nodes, prefix) {
   function attr(v) { return '"' + ('' + v).replace(/"/, '&') + '"'; }

   var s = '';
   if (!prefix) {
      s += '<?xml version="1.0" encoding="utf-8" ?>\n<tree>\n';
      s += toXML(nodes, '  ');
      s += '</tree>\n';
   } else {
      for (var i = 0; i < nodes.length; i++) {
         if (!nodes[i]) continue;
         var children = nodes[i].slice(3);
         s += prefix + '<node';
         if (nodes[i][0]) s += ' caption=' + attr(nodes[i][0]);
         if (nodes[i][1]) s += ' url=' + attr(nodes[i][1]);
         if (nodes[i][2]) s += ' target=' + attr(nodes[i][2]);
         if (children.length) {
            s += '>\n';
            s += toXML(children, prefix + '  ');
            s += prefix + '</node>\n';
         } else
            s += ' />\n';
      }
   }
   return s;
}

var xml = toXML(TREE_NODES);

document.write('<pre>' + xml.replace(/</g, '&').replace(/>/g, '&') + '</pre>');
</script>

For me it works like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<tree>
  <node caption="Scripts">
    <node caption="COOLjsTree" url="http://javascript.cooldev.com/scripts/cooltree/" />
    <node caption="COOLjsMenu" url="http://javascript.cooldev.com/scripts/coolmenu/" />
  </node>
  <node caption="Online help">
    <node caption="COOLjsTree" url="http://javascript.cooldev.com/scripts/cooltree/docs/" />
    <node caption="COOLjsMenu" url="http://javascript.cooldev.com/scripts/coolmenu/docs/" />
  </node>
  <node caption="Download">
    <node caption="Free" />
    <node caption="Order" />
  </node>
</tree>
Back to top
Display posts from previous:   
Post new topic   Reply to topic    JavaScript.CoolDev.Com Forum Index -> COOLjsTree All times are GMT
Page 1 of 1