Read XML tree into treectrl?

44 views
Skip to first unread message

Heck Lennon

unread,
May 15, 2024, 6:34:03 AMMay 15
to beautifulsoup
Hello,

I need to read one or more XML files into a TreeCtrl in wxPython.

Is there a ready-to-use solution instead of looping through each node and call AppendItem()?

Thank you.

BS.treectrl.XML.png

Chris Papademetrious

unread,
May 15, 2024, 11:00:14 AMMay 15
to beautifulsoup
Hello,

In your XML files, are there any non-whitespace text nodes with data, or is all the data contained in element nodes and attributes? Does every XML file become a separate top-level list item? Are you creating the sub-list structure exactly after the XML element structure?

 - Chris

Heck Lennon

unread,
May 15, 2024, 12:01:54 PMMay 15
to beautifulsoup
> In your XML files, are there any non-whitespace text nodes with data, or is all the data contained in element nodes and attributes?

I don't know that "non-whitespace text nodes"  means. Would you have an example?

> Does every XML file become a separate top-level list item?

Yes. They'll files selected through a dialog box.

> Are you creating the sub-list structure exactly after the XML element structure?

Not sure what it means either. I'd like to just copy the whole contents of each file into its own root element, so that the user can see the exact, full structure in the tree than it's in the source files.

Chris Papademetrious

unread,
May 15, 2024, 1:26:27 PMMay 15
to beautifulsoup
By non-whitespace text nodes, I mean text content inside an element itself:

<p>This element contains text.</p>

I don't know the answer to your question; I was just trying to understand the problem. The BeautifulSoup part of walking the tags hierarchically isn't hard, if it comes to that.

 - Chris

Heck Lennon

unread,
May 15, 2024, 3:01:39 PMMay 15
to beautifulsoup
Nope, GPX files only contain attributes and sub-elements:

========
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1">
  <trk>
<name>blah</name>
<trkseg>
<trkpt lat="46.987879" lon="3.157958">
</trkpt>
  <trkpt lat="46.987618" lon="3.158219">
</trkpt>
</trkseg>
</trk>
</gpx>
========

I'll experiment.

Thank you.

Chris Papademetrious

unread,
May 15, 2024, 3:24:52 PMMay 15
to beautifulsoup
I don't know the UI library you mentioned, but I've used a pattern like this for similar tasks:

def make_ui_thing(from: bs4.Tag, into: ???):
  new_ui_thing = ???
  for child_tag in from.find_all(True, recursive=False):
    new_ui_thing.add(make_ui_thing(from=child_tag, into=new_ui_thing))

where a function creates some "thing" for a tag, then calls itself recursively to create and insert the children "things" into that "thing".

 - Chris

Heck Lennon

unread,
May 15, 2024, 3:52:34 PMMay 15
to beautifulsoup
Thx!
Reply all
Reply to author
Forward
0 new messages