java - Get all hierarchical root elements name for an xml child -
i have xml file below:
<?xml version="1.0" encoding="utf-8"?> <!doctype xml> <root> <menu title="menu 1" url="root_pages.php?id=1"> <submenu title="sub 1" url="pages.php?id=1" /> <submenu title="sub 2" url="pages.php?id=2" /> <submenu title="sub 3" url="pages.php?id=3" /> <submenu title="sub 4" url="pages.php?id=4" /> <submenu title="sub 5" url="pages.php?id=5" /> </menu> <menu title="menu 2" url="root_pages.php?id=2"> <submenu title="sub 6" url="pages.php?id=6" /> <submenu title="sub 7" url="pages.php?id=7" /> </menu> <menu title="menu 3" url="root_pages.php?id=3"> </menu> </root>
now trying every element can full hierarchy path (like breadcrumb).
for example if take <submenu title="sub 3>
element, result : root/menu 1/sub3
is there simple , easy way this?
thanks, surodip
var data = $.parsexml($("pre").html()); var xml = $(data.documentelement); var res = ""; var elem = xml.find("[title='sub 3']"); elem.parents().add(elem).each(function(i, el) { res += ($(el).attr("title") || el.tagname) + "/" }); $("body").prepend(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <pre> <root> <menu title="menu 1" url="root_pages.php?id=1"> <submenu title="sub 1" url="pages.php?id=1"></submenu> <submenu title="sub 2" url="pages.php?id=2"></submenu> <submenu title="sub 3" url="pages.php?id=3"></submenu> <submenu title="sub 4" url="pages.php?id=4"></submenu> <submenu title="sub 5" url="pages.php?id=5"></submenu> </menu> <menu title="menu 2" url="root_pages.php?id=2"> <submenu title="sub 6" url="pages.php?id=6"></submenu> <submenu title="sub 7" url="pages.php?id=7"></submenu> </menu> <menu title="menu 3" url="root_pages.php?id=3"> </menu> </root> </pre>
Comments
Post a Comment