在 XML 文件中查找 children 的方法比这更好吗?
Better way to find children in XML file than this?
所以我有一些不同的 XML 文件,它们并不都具有相同的格式或数据元素。我想编写一些代码来获取所有不同类型节点的列表,方法是检查每个节点并查看它是否具有 children,然后检查 children 的 children,等等。
我可以写出来,所以它以静态方式执行,但我宁愿让它测试以查看每个节点中是否有一个节点,并继续向下钻取。
例如我是这样做的:
import xml.etree.ElementTree as ET
tree = ET.iterparse('vx19.xml')
for _, el in tree:
if '}' in el.tag:
el.tag = el.tag.split('}', 1)[1] # strip all namespaces
for at in el.attrib.keys(): # strip namespaces of attributes too
if '}' in at:
newat = at.split('}', 1)[1]
el.attrib[newat] = el.attrib[at]
del el.attrib[at]
root = tree.root
for a in root:
print(a.tag)
if a[0]:
for b in a:
print('\t',b.tag)
for c in b:
print('C')
print('\t\t',c.tag)
for d in c:
print('D')
print('\t\t\t',d.tag)
for e in d:
print('E')
print('\t\t\t\t',e.tag)
这给了我正在寻找的东西,但必须有更好的方法来写这个。
B
Method Def
C
TriggerOn
C
Formal Expression
C
Form Ref
C
Actions
D
Identifiers
E
Identifier
E
Identifier
E
Identifier
我希望写一些类似的东西,"In this node, check if there are sub-nodes. If not, end. If so, then list out the sub-nodes, AND for each sub-node, are there further sub-nodes? If so....repeat on down until there are no new layers to drill down into."
谢谢!!
如果您的输入有一个已知的嵌套数量限制,并且该数量小于 cpython
解释器的 max recursion limit
,您可以使用简单的递归来实现该行为,否则您'将使用列表和迭代算法。
迭代:
def iterative(root, result: set):
queue = [root]
for item in queue:
result.add(item.tag)
for elem in item:
queue.append(elem)
return result
递归:
def recursive(root, result: set):
for item in root:
result.add(item.tag)
for elem in item:
recurse(item, result)
return result
所以我有一些不同的 XML 文件,它们并不都具有相同的格式或数据元素。我想编写一些代码来获取所有不同类型节点的列表,方法是检查每个节点并查看它是否具有 children,然后检查 children 的 children,等等。
我可以写出来,所以它以静态方式执行,但我宁愿让它测试以查看每个节点中是否有一个节点,并继续向下钻取。
例如我是这样做的:
import xml.etree.ElementTree as ET
tree = ET.iterparse('vx19.xml')
for _, el in tree:
if '}' in el.tag:
el.tag = el.tag.split('}', 1)[1] # strip all namespaces
for at in el.attrib.keys(): # strip namespaces of attributes too
if '}' in at:
newat = at.split('}', 1)[1]
el.attrib[newat] = el.attrib[at]
del el.attrib[at]
root = tree.root
for a in root:
print(a.tag)
if a[0]:
for b in a:
print('\t',b.tag)
for c in b:
print('C')
print('\t\t',c.tag)
for d in c:
print('D')
print('\t\t\t',d.tag)
for e in d:
print('E')
print('\t\t\t\t',e.tag)
这给了我正在寻找的东西,但必须有更好的方法来写这个。
B
Method Def
C
TriggerOn
C
Formal Expression
C
Form Ref
C
Actions
D
Identifiers
E
Identifier
E
Identifier
E
Identifier
我希望写一些类似的东西,"In this node, check if there are sub-nodes. If not, end. If so, then list out the sub-nodes, AND for each sub-node, are there further sub-nodes? If so....repeat on down until there are no new layers to drill down into."
谢谢!!
如果您的输入有一个已知的嵌套数量限制,并且该数量小于 cpython
解释器的 max recursion limit
,您可以使用简单的递归来实现该行为,否则您'将使用列表和迭代算法。
迭代:
def iterative(root, result: set):
queue = [root]
for item in queue:
result.add(item.tag)
for elem in item:
queue.append(elem)
return result
递归:
def recursive(root, result: set):
for item in root:
result.add(item.tag)
for elem in item:
recurse(item, result)
return result