DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: How to extract tree hierarchy in xml?  (Read 9,734 times)

kalos

  • Member
  • Joined in 2006
  • **
  • Posts: 1,824
How to extract tree hierarchy in xml?
« on: July 25, 2018, 01:09 PM »
Hello!

You know that xml files are hierarchical, ie there is a top level attribute, then sub-attributes are indented and so on.

How can I copy the hierarchy, ie the upper level attributes of a selected attribute/line?

For example:

<a>
    <1>
    <2>
          <x1>
          <x2>

I want to select x2 and copy the hierarchy of it, ie the upper level attributes that x2 belongs to:
<a>
    <2>


Any idea?

Thanks!

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: How to extract tree hierarchy in xml?
« Reply #1 on: July 25, 2018, 05:49 PM »
If this is something you're going to need often, you can look into XSLT - it lets you transform XML files in pretty flexibly ways.

It's pretty clunky to work with, though, so if you need a quick solution for a specific problem, there's probably easier ways.
- carpe noctem

kalos

  • Member
  • Joined in 2006
  • **
  • Posts: 1,824
Re: How to extract tree hierarchy in xml?
« Reply #2 on: July 30, 2018, 02:55 PM »
Thanks, as for now, is there an XML reader that can extract the hierarchy tree of a specific line?

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: How to extract tree hierarchy in xml?
« Reply #3 on: July 30, 2018, 05:41 PM »
is there an XML reader that can extract the hierarchy tree of a specific line?
I use Notepad++ with plug-ins "XML Tools" and "Npp XML Treeview". The XML Tools plug-in has a nice and quick formatting feature (Ctrl-Alt-Shift-B), XPath expression evaluation and it shows the path to the current node when pressing Ctrl-Alt-Shift-P. With the treeview plug-in you can see the structure of the xml nodes (but not the attributes), and quickly select a node.

If that doesn't suffice, there's always XML Copy Editor, an open source specialized XML editor with a lot of features, including XSLT and XPath support. I hardly use it but it does offer some features not easily available in Notepad++ with it's plug-ins.
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: How to extract tree hierarchy in xml?
« Reply #4 on: July 30, 2018, 07:04 PM »
You could also use an online one.

https://codebeautify.org/xmlviewer

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: How to extract tree hierarchy in xml?
« Reply #5 on: July 30, 2018, 08:01 PM »
And if the OP feels really adventurous, he could try to use PowerShell, it seems to handle XML quite nicely, like I found in this ServerFault item, but it helps if you speak XPath, I guess...

You could also use an online one.

https://codebeautify.org/xmlviewer
It doesn't have much of the features the OP asked, though, and I'm a bit worried how/if it will handle a large xml file...
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: How to extract tree hierarchy in xml?
« Reply #6 on: July 31, 2018, 12:53 AM »
I've used their JSON viewer on a large file before (7MB+) and it handled it with aplomb.  It doesn't extract the hierarchy, but it does visualize it, so he can know what to copy from the tree.  Just a suggestion.

Original address: https://www.donationcoder.com/forum/index.php?topic=45913.0