向元素添加相同的标记元素

2024-09-27 23:21:42 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在尝试将元素添加到解析为元素对象的xml文件中。 元素具有相同的标记并且处于相同的级别

正如您在输出中所看到的那样,在Installationis append中只有一个元素,我希望能够按照自己的意愿进行追加

谢谢你的帮助

这是我试图修改的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>

Tags: falsetrueinstallationxmlconditionsadressecomplementmesure
1条回答
网友
1楼 · 发布于 2024-09-27 23:21:42

请加上

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

那么它应该会起作用

相关问题 更多 >

    热门问题