使用BeautifulSoup显示XML树结构

当使用新的XML结构时,首先查看全局视图总是有帮助的。

When loading it with BeautifulSoup:

import requests, bs4
s = requests.get('https://www.w3schools.com/xml/cd_catalog.xml').text
x = bs4.BeautifulSoup(s, 'xml')
print(x)

有没有内置的方法来显示不同深度的树结构?

Example for https://www.w3schools.com/xml/cd_catalog.xml, with maxdepth=0, it would be:

CATALOG

with maxdepth=1, it would be:

CATALOG
  CD 
  CD
  CD
  ...

and with maxdepth=2, it would be:

CATALOG
  CD 
    TITLE
    ARTIST
    COUNTRY
    COMPANY
    PRICE
    YEAR
  CD 
    TITLE
    ARTIST
    COUNTRY
    COMPANY
    PRICE
    YEAR
  ...
评论