add same tag Element to Element

Viewed 20

Hello i m trying to add elements to a xml file parsed as Element object. elements have same tag and are at the same level.

As you can see in the output only one element intallationis append i would like to be able to append as much as i want.

Thank for your help.

here is the xml file i 'm trying to modify

'''xml
<mesures>
    <mesure>
        <refMesure>LEM180895</refMesure>
        <conditions interieur="true" perimetreSecurite="false" champLointain="true">
            <duree dateDebut="2018-07-24" heureDebut="10:00" dateFin="2018-07-24" heureFin="12:30"/>
            <typeEnvironnement>PUBLIC</typeEnvironnement>
            <complement></complement>
        </conditions>
        <localisation changementAdresse="false">
            <motifChangementAdresse/>
            <position lon="2.3227499999999996" lat="48.8665556" hauteurSol="0"/>
            <adresse>
                <voie></voie>
                <lieudit></lieudit>
                <complement>string</complement>
                <codePostal>00000</codePostal>
                <commune>Commune</commune>

            </adresse>

        </localisation>
        <installationsVisibles>

        </installationsVisibles>
        <resultat respectNiveauReference="false">
            <valeurMoyenne>0</valeurMoyenne>
            <!--You have a CHOICE of the next 3 items at this level-->
            <!--Optional:-->
        </resultat>
    </mesure>
</mesures>
'''

Here is the code i used

from lxml import etree
from lxml import objectify


fileobject = open('C:/Users/gustu/Dropbox/RATP2/uploadANFR/ANFRAutoXML/3 - Mise à jour schéma echange ANFR/Schema_Echanges_MCR/anfr_laboratoire/aideInternet/extract.xml','r')
tree = objectify.parse(fileobject)
root=tree.getroot()




# xml='''
# <installationVisibles>
# </installationVisibles>
# ''' 

# installationVisibles = objectify.fromstring(xml)




installation=objectify.fromstring("<installation hauteur=\"8\" distance=\"9\"><type></type></installation>")

root.mesure.installationsVisibles.insert(0,installation)
root.mesure.installationsVisibles.insert(1,installation)
print(etree.tostring(root))

<mesures><mesure><refMesure>LEM180895</refMesure><conditions 
interieur="true" perimetreSecurite="false" champLointain="true"><duree dateDebut="2018-07-24" heureDebut="10:00" dateFin="2018-07-24" heureFin="12:30"/><typeEnvironnement>PUBLIC</typeEnvironnement><complement/></conditions><localisation changementAdresse="false"><motifChangementAdresse/><position lon="2.3227499999999996" lat="48.8665556" hauteurSol="0"/><adresse><voie/><lieudit/><complement>string</complement><codePostal>00000</codePostal><commune>Commune</commune></adresse></localisation><installationsVisibles>\n\n        <installation hauteur="8" distance="9"><type/></installation></installationsVisibles><resultat respectNiveauReference="false"><valeurMoyenne>0</valeurMoyenne><!--You have a CHOICE of the next 3 items at this level--><!--Optional:--></resultat></mesure></mesures>

Output

<mesures>
    <mesure>
        <refMesure>LEM180895</refMesure>
        <conditions interieur="true" perimetreSecurite="false" champLointain="true">
            <duree dateDebut="2018-07-24" heureDebut="10:00" dateFin="2018-07-24" heureFin="12:30"/>
            <typeEnvironnement>PUBLIC
            </typeEnvironnement>
            <complement/>
        </conditions>
        <localisation changementAdresse="false">
            <motifChangementAdresse/>
            <position lon="2.3227499999999996" lat="48.8665556" hauteurSol="0"/>
            <adresse>
                <voie/>
                <lieudit/>
                <complement>string</complement>
                <codePostal>00000</codePostal>
                <commune>Commune</commune>
            </adresse>
        </localisation>
        <installationsVisibles>\n\n        
            <installation hauteur="8" distance="9">
                <type/>
            </installation>
        </installationsVisibles>
        <resultat respectNiveauReference="false">
            <valeurMoyenne>0</valeurMoyenne>            <!--You have a CHOICE of the next 3 items at this level-->            <!--Optional:--></resultat>
    </mesure>
</mesures>
1 Answers

Please add

from copy import copy

and replace

root.mesure.installationsVisibles.insert(0, installation)
root.mesure.installationsVisibles.insert(1, installation)

with

root.mesure.installationsVisibles.insert(0, copy(installation))
root.mesure.installationsVisibles.insert(1, copy(installation))

Then it should work.

Related