16

Any way to "collapse all" all nodes in one click in firefox or chrome and then able to search a node's name and then quickly see its path/tree of parent nodes? First collapse not just top node, but all child nodes, and when I search for a node then it opens only that tree of nodes. I want this because I search a node in a big xml file. The node is present many times under different paths of parent nodes and I need to see the path of the node where ever it appears. Right now I have to manually traverse whole file and collapse many times to find all paths of that node that is present many times in a file. Any quick way of doing it?

Computer User
  • 2,839
  • 4
  • 47
  • 69
  • You can write a code snippet which automatically clicks on all "Collapse" icons. This takes just a few lines, and can be implemented as a bookmarklet. – Rob W Jun 16 '13 at 08:42
  • I will prefer a ready-made plugin/add-on first before writing code. Thanks, anyway. – Computer User Jun 16 '13 at 08:45
  • 1
    Chrome XML Viewer extension? https://chrome.google.com/webstore/detail/xv-%E2%80%94-xml-viewer/eeocglpgjdpaefaedpblffpeebgmgddk?hl=en – Jaywalker Oct 25 '13 at 14:37
  • Related question: [Better XML viewing in Firefox](/q/972914) – cachius Aug 22 '22 at 13:26

2 Answers2

13

To collapse all XML nodes in Chrome, execute this in your JavaScript console

var nodes = document.getElementsByClassName("button collapse-button");
for (var i = 0; i < nodes.length; i++) {
    nodes[i].click();
}
CodigosTutoriales
  • 1,018
  • 1
  • 10
  • 21
  • 4
    One liner: `Array.from(document.getElementsByClassName("collapse-button")).forEach(el => el.click())` – Velda Nov 28 '19 at 15:18
  • 2
    fix for latest chrome: `Array.from(document.getElementsByClassName("folder-button fold")).forEach(el => el.click())` – guymguym Oct 29 '20 at 15:40
4

If firefox or chrome are not a strict requirement, I suggest geany, in particular its pretty-printer plugin. With it you can simply format the file in a human-friendly way, fold and un-fold all elements in a single click and so on.

If you are on debian or ubuntu:

sudo apt-get install geany geany-plugin-prettyprinter
mrfree
  • 141
  • 3
  • I installed geany but couldn't manage to fold all elements with one click. – pagliuca Sep 20 '22 at 14:28
  • Open your XML file, then click `Tools->PrettyPrinter XML`. Finally, try with `Fold All` and `Unfold All` under `Document`. I'm using geany-1.38 – mrfree Dec 12 '22 at 17:37
  • works to some extent. I have a file with 320000 lines. After folding I cannot see the end of the document, in fact not further than 36000 lines. Geany v1.38. EDIT: Its getting stuck on a CDATA – caduceus Jan 19 '23 at 11:27