Last active
December 28, 2015 10:29
-
-
Save S4lt5/7486284 to your computer and use it in GitHub Desktop.
jQuery XML visualization -- display block representation of XML string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <style> | |
| #treeView li { | |
| background-color:lightgray; | |
| text-align: left; | |
| width:7em; | |
| list-style: none; | |
| border: 1px outset gray; | |
| margin-bottom: 2px; | |
| } | |
| li.nochildren { | |
| background-color:white !important; | |
| color:green; | |
| border:none !important; | |
| } | |
| #treeView ul { | |
| margin-left: 10px; | |
| padding-left: 7em; | |
| } | |
| #treeView b { | |
| padding-left: 4px; | |
| padding-right:4px; | |
| float:left; | |
| } | |
| </style> | |
| <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> | |
| <script > | |
| $(document).ready(function () { | |
| var xml = '<hi><one /><two>2.0</two><three>asdf<three-point-five /></three>Lorem Ipsum blah blah</hi>'; | |
| var xml = $(xml); | |
| maketree(xml); | |
| function maketree(tree) { | |
| if (tree.children(":first").length > 0 && tree[0]) { | |
| traverse($('#treeView li'), tree[0]); | |
| } | |
| $('<b>-<\/b>').prependTo('#treeView li:has(li)').click(function () { | |
| if ($(this).text() === '-') | |
| { | |
| $(this).text('+'); | |
| $(this).next().children().hide(); | |
| } | |
| else | |
| { | |
| $(this).text('-'); | |
| $(this).next().children().show(); | |
| } | |
| }); | |
| } | |
| function traverse(node, tree) { | |
| var children = $(tree).contents() | |
| node.append(tree.nodeName); | |
| if (children.length > 0) { | |
| var ul = $("<ul>").appendTo(node) | |
| children.each(function () { | |
| var li = $('<li>').appendTo(ul); | |
| if (this.nodeType == 3) { | |
| li.addClass('nochildren'); | |
| li.text(this.textContent); | |
| } else { | |
| traverse(li, this); | |
| } | |
| }) | |
| } | |
| } | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <ul id="treeView"> | |
| <li></li> | |
| </ul> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment