добавить тот же тег элемент в элемент - PullRequest
2 голосов
/ 09 октября 2019

Здравствуйте, я пытаюсь добавить элементы в XML-файл, проанализированный как объект Element. элементы имеют одинаковый тег и находятся на одном уровне.

Как видно из вывода, добавляется только один элемент. Я хотел бы иметь возможность добавлять столько, сколько я хочу.

Спасибоза вашу помощь.

вот XML-файл, который я пытаюсь изменить

'''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>
'''

Вот код, который я использовал

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>

Вывод

<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 Ответ

0 голосов
/ 09 октября 2019

Пожалуйста, добавьте

from copy import copy

и замените

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

на

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

Тогда это должно работать.

...