简体   繁体   中英

Element does not exist when I want to remove it

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<?xml-stylesheet href="Famille.xsl" type="text/xsl"?><HF_DOCUMENT>
    <Data>
        <CodeFamille>12 BIS</CodeFamille>
        <Libelle>12 bis</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>3700744900020</CodeFamille>
        <Libelle>BALLON GONFLABLE</Libelle>
        <Livre>0</Livre>
    </Data>
    <Data>
        <CodeFamille>4634510541685140</CodeFamille>
        <Libelle>AKATA</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>707003</CodeFamille>
        <Libelle>Hacomo</Libelle>
        <Livre>0</Livre>
    </Data>
    <Data>
        <CodeFamille>9782756076430</CodeFamille>
        <Libelle>Draw tome 2</Libelle>
        <Livre>0</Livre>
    </Data>
    <Data>
        <CodeFamille>ACCESSOIRE</CodeFamille>
        <Libelle>Figurine</Libelle>
        <Livre>0</Livre>
    </Data>
    <Data>
        <CodeFamille>AKIKO</CodeFamille>
        <Libelle>Akiko</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>AKILEOS</CodeFamille>
        <Libelle>Akileos</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>AKUMA</CodeFamille>
        <Libelle>Akuma</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ALBIN MICHEL</CodeFamille>
        <Libelle>Albin Michel</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ALIMENTATION</CodeFamille>
        <Libelle>Alimentation 5.5</Libelle>
        <Livre>0</Livre>
    </Data>
    <Data>
        <CodeFamille>AMALTHEE</CodeFamille>
        <Libelle>Amalthee</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ANIME MANGA PRESSE</CodeFamille>
        <Libelle>Anime Manga Presse</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ANKAMA</CodeFamille>
        <Libelle>Ankama</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ARTEFAC</CodeFamille>
        <Libelle>Artefac</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ASIAN DISTRICT</CodeFamille>
        <Libelle>Asian district</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ASSIMIL</CodeFamille>
        <Libelle>Assimil</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ASUKA</CodeFamille>
        <Libelle>ASUKA</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ATOMIC CLUB</CodeFamille>
        <Libelle>Atomic club</Libelle>
        <Livre>1</Livre>
    </Data>
    <Data>
        <CodeFamille>ATRABILE</CodeFamille>
        <Libelle>Atrabile</Libelle>
        <Livre>1</Livre>
    </Data>
    ...
</HF_DOCUMENT>

When I run a simple loop to look into a XML file, I can find every children, 203 in total.

In all those children, I want to remove those with <Livre>0</Livre> which represent 63 in total.

My problem is that when I run the same loop with a simple condition to remove only those who respect the condition, I can not remove all of them, only 43 are removed.

<Data>
    <CodeFamille>9782756076430</CodeFamille>
    <Libelle>Draw tome 2</Libelle>
    <Livre>0</Livre>
</Data>
...
<Data>
    <CodeFamille>ALIMENTATION</CodeFamille>
    <Libelle>Alimentation 5.5</Libelle>
    <Livre>0</Livre>
</Data>

Those nodes for example while stay.

Here is my code:

tree = ET.parse("test.xml")
root = tree.getroot()

for child in root:
    if child.find('Livre').text != "1":
        root.remove(child)

tree.write("output.xml")

Change the for loop like this so it iterates over a list and then deleting individual elements doesn't get confusled.

for child in list(root):
    if child.find('Livre').text != "1":
        root.remove(child)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM