我的XML:
<animals>
<animal name="fox" fullname="fullfox"></animal>
<animal name="dog" fullname="halfdog"><food>milk</food><food>rice</food><food>meat</food> </animal>
<animal name="cow" fullname="doublecow"><food>grass</food></animal>
<animal name="blabla" fullname="fullbla"></animal>
</animals>
我正在尝试解析此XML以获得与输出相同的XML。
doc = ET.parse("an.xml")
root = doc.getroot() #Returns the root element for this tree.
root_new = ET.Element("animals")
for child in root:
name = child.attrib['name']
fullname = child.attrib['fullname']
for g in root.findall("*/food"):
animal = ET.SubElement(root_new, "animal")
food = ET.SubElement(animal, "food")
food.text = g.text
animal.set("name",name)
animal.set("fullname",fullname)
tree = ET.ElementTree(root_new)
tree.write(sys.stdout)
但我只得到最后一个值
<animals>
<animal fullname="fullbla" name="blabla"><food>milk</food></animal>
<animal fullname="fullbla" name="blabla"><food>rice</food></animal>
<animal fullname="fullbla" name="blabla"><food>meat</food></animal>
<animal fullname="fullbla" name="blabla"><food>grass</food></animal>
</animals>
而且食物节点也错了,如何迭代就像我的输入XML一样?
答案 0 :(得分:2)
您需要一个嵌套循环:
for child in root:
name = child.attrib['name']
fullname = child.attrib['fullname']
# create "animal" here
animal = ET.SubElement(root_new, "animal")
animal.set("name",name)
animal.set("fullname",fullname)
for g in child.findall("food"):
# create "food" here
food = ET.SubElement(animal, "food")
food.text = g.text
答案 1 :(得分:2)
您的代码应如下所示
doc = ET.parse("test.xml")
root = doc.getroot() #Returns the root element for this tree.
root_new = ET.Element("animals")
for child in root:
name = child.attrib['name']
fullname = child.attrib['fullname']
animal = ET.SubElement(root_new, "animal")
animal.set("name",name)
animal.set("fullname",fullname)
for g in child.findall("food"):
food = ET.SubElement(animal, "food")
food.text = g.text
tree = ET.ElementTree(root_new)
tree.write(sys.stdout)
答案 2 :(得分:1)
有两个问题。第一个是你的缩进 - 我认为这些是嵌套循环。第二个问题是您正在使用root.findall
,这意味着无论他们在哪个节点,您都会获得所有food
个项目。请尝试使用此项:
...
for child in root:
name = child.attrib['name']
fullname = child.attrib['fullname']
animal = ET.SubElement(root_new, 'animal')
for g in child.findall("food"):
food = ET.SubElement(animal, "food")
food.text = g.text
animal.set('name', name)
animal.set('fullname', fullname)